美文网首页
Android View点击效果及背景颜色

Android View点击效果及背景颜色

作者: 懵懵懂懂_YOYO | 来源:发表于2022-05-17 19:31 被阅读0次

1.在res目录下的drawable文件夹下创建点击效果文件bg_blue_selector.xml

<?xml version="1.0" encoding="utf-8"?><!--<shape xmlns:android="http://schemas.android.com/apk/res/android"-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle" android:useLevel="false">
            <corners android:radius="80dp" />
            <solid android:color="#505464" />
        </shape>
    </item>
    <item android:state_pressed="false">
        <shape android:shape="rectangle" android:useLevel="false">
            <corners android:radius="80dp" />
            <solid android:color="#35405c" />
        </shape>
    </item>
</selector>

2.在布局中使用:

<TextView
            android:id="@+id/tvCancle"
            android:layout_width="130dp"
            android:layout_height="35dp"
            android:background="@drawable/bg_blue_selector"
            android:gravity="center"
            android:text="取消"
            android:textColor="@color/white"
            android:textSize="15sp" />

3.效果如下

点击前:


image.png

点击后:


image.png

4.背景色效果变化

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="#11000000"/>
        </shape>
    </item>
     <item android:state_pressed="false">
        <shape android:shape="rectangle">
            <!-- 透明色 -->
            <solid android:color="#00000000"/>
        </shape>
    </item>
 
</selector>

相关文章