The click effect of the android button and the click effect of the layout are written in xml

In the process of android development and learning, in order to increase the good interactivity of the user interface, we will add effects to the click of the button to tell the user that the click is successful.

Generally change the color of the button or change the background picture of the button by clicking before and after:

Before clicking: After clicking:


Use xml for control

<Button
            android:id="@+id/bt_login"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:gravity="center"
            android:background="@drawable/btn_green"
            android:text="登陆"
            android:textColor="#fff" />



btn_green.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/left_menulayout_click" 
        android:state_pressed="true"/>
	
    <item android:drawable="@color/unleft_menulayout_click" 
        android:state_pressed="false"/>

</selector>


Call a color in colo.xml when the click is true: left_menulayout_click

Call a color in colo.xml when the click is false: unleft_menulayout_click This completes the effect of clicking the color change


2. For setting the button to rounded corners

Look at the following piece of code:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    
    <item android:state_pressed="false">
        <shape>
            <solid android:color="#fff"/>
             <corners android:radius="5dip" />
            <stroke android:width="0.5dp" android:color="#C8C8C8"/>
        </shape>
    </item>
    
    <item android:state_pressed="true">
        <shape>
            <solid android:color="#ebe9e9"/>
             <corners android:radius="5dip" />
            <stroke android:width="0.5dp" />
        </shape>
    </item>
 
</selector> 

<solid android:color="#fff"/> is the color that represents the area inside the rounded corners

<corners android:radius="5dip" /> is to control the radius of the rounded corners

<stroke android:width="0.5dp" android:color="#C8C8C8"/> is to control the width and color of the rounded stroke, as shown below:



Guess you like

Origin blog.csdn.net/u013521274/article/details/50659005