美文网首页
Android点击效果背景

Android点击效果背景

作者: Carrot_123 | 来源:发表于2018-10-19 10:02 被阅读0次

创建shape

创建两个shape文件分别作为点击按下和抬起的显示效果。
press_bg.xml 内容如下:

 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorGray" />
    <!-- 边框 -->
    <stroke
        android:width="2dp"
        android:color="@android:color/holo_red_light" />
    <corners
        android:topLeftRadius="10dip"
        android:topRightRadius="10dip"
        android:bottomLeftRadius="10dip"
        android:bottomRightRadius="10dip" />
 </shape>

up_bg.xml 内容如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/black" />
    <!-- 边框 -->
    <stroke
        android:width="2dp"
        android:color="@android:color/holo_green_light" />
    <corners
        android:topLeftRadius="10dip"
        android:topRightRadius="10dip"
        android:bottomLeftRadius="10dip"
        android:bottomRightRadius="10dip" />
</shape>

创建selector

创建selector文件button_bg.xml,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/up_bg.xml" android:state_pressed="false"/>
    <item android:drawable="@drawable/press_bg.xml" android:state_pressed="true"/>
</selector>

引用selector

在layou文件中控件引用上述的selector作为背景,运行后点击button,可发现按下和抬起的不同效果。

 <Button
        android:text="Hello"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content" 
        android:background="@drawable/button_bg"
        />

如果对您有所帮助请给在下点个赞

相关文章

网友评论

      本文标题:Android点击效果背景

      本文链接:https://www.haomeiwen.com/subject/rwxyzftx.html