Android Beginners III to Button

Today share with you, I will learn in the process Button learned.
Button and some other commonly used controls are TextView subclass of so many properties and also similar TextView.
For
example: . . . .
These are Android beginner two of TextView have introduced this article.
Let me share some of the major differences between Button for the TextView.
In Android Studio, we can set a variety of styles Button by using Drawable.
Figure right mouse button to create a Drawable. Here Insert Picture Description
1, round buttons
First we build a Drawable, code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid
    	<!--设定填充色-->
        android:color="#303F9F"
        />
    <corners
    	<!--设定圆角的半径-->
        android:radius="25dp"
        />
</shape>

The definition of a type Shape Drawable. Define this file is named: button_shape.xml

     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Button"
     android:textAllCaps="false"
     <!--这里引用上方的定义的button_shape.xml,将背景和形状设定为自定义的-->
     android:background="@drawable/button_shape"/>

Then, look at the effect of it. Here Insert Picture Description
A little bit too round, specific to the actual situation may be set according to their own property inside corners of radious.
corners there are four other properties.
topLeftRadius : the top left
topRightRadius : top right
buttomLeftRadius : bottom left
buttonRigthRadius : the lower right corner
are provided four fillet radius.
2, the blank button
is also establishing a Drawable, named button_storke, directly on the code.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke
        android:width="2dp"
        android:color="#303F9F"
        />
    <corners
        android:radius="20dp"/>
</shape>

And rounded buttons , like the file is assigned to the drawable background:

android:background="@drawable/edittext_stroke"/>

Display as shown in FIG: Here Insert Picture Description
3, Picture button
image button, i.e. a background image is filled into the code as follows.

    <Button
        android:id="@+id/Button_bt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/Button_bt1"
        android:layout_marginTop="10dp"
        android:background="@mipmap/user"
        android:text="Button"
        android:textAllCaps="false" />

FIG display:
Here Insert Picture Description
mismatch if the picture size and the same button shaped like FIG, re-edit button border, or re-modify the shape of the picture. Or find the right picture of it. Recommended easyicon and Ali icon library you can find what you want small icons.
4, the pressing effect of the button with
the same type of button and more. Establish a Drawable file. code show as below:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <solid android:color="#FF00F1"/>
        </shape>
    </item>
    <item android:state_pressed="false">
        <shape>
            <solid android:color="@color/colorAccent"/>
        </shape>
    </item>
</selector>

Note that this time attributes of the file should be Slecter, rather Shape. Button code obtaining a current state to change the color of the background of the Button.
Display as follows:
Here Insert Picture Description
5 buttons with corrugated
// to be updated, is expected to 5 days ...

Released seven original articles · won praise 4 · Views 2297

Guess you like

Origin blog.csdn.net/weixin_43761339/article/details/105201963