Android development: use selector to customize View style

The controls of the original Android are really not very good-looking. But we can make the style we want by using selector.
Here is an example. Of course the example would be ugly. Just play a role in attracting new ideas.
The effect is as follows:
Write picture description here

Create a new button_shape.xml in the drawable folder:

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 没有焦点 -->
    <item android:state_window_focused="false">
        <shape>
            <!-- 背景颜色 -->
            <solid android:color="#456E02"/>
            <!-- 描边 -->
            <stroke android:width="1dp" android:color="#000000"/>
            <!-- 圆角 -->
            <corners android:radius="5dp"/>
            <!-- 边距 -->
            <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
        </shape>
    </item>

    <!-- 触摸模式下单击-->
    <item android:state_focused="false" android:state_pressed="true">
        <shape>
            <!-- 背景颜色 -->
            <solid android:color="#45C01A" />
            <!-- 描边 -->
            <stroke android:width="2dp" android:color="#000000"/>
            <!-- 圆角 -->
            <corners android:radius="10dp"/>
            <!-- 边距 -->
            <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
        </shape>
    </item>
</selector>

Then set the style for the button:

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_shape"
        android:text="按钮" />

Guess you like

Origin blog.csdn.net/new_Aiden/article/details/50876553