Android Button点击效果(按钮背景变色、文字变色)

这篇博客介绍了如何在Android中实现Button点击时的文字颜色和背景颜色变化效果,提供了通过XML定义的方法。文章包括具体步骤,涉及到在color.xml中定义颜色,创建drawable目录下的selector文件,并在布局文件中应用statelist drawable。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们做应用有时需要点击按钮时的文字颜色变换效果,目前发现两种解决方案:第一用图片,如果出现的地方比较多,那么图片的量就相当可观;第二,也就是本文讲到的 xml 定义方式。
下面先上效果图:

正常时的文字颜色:
按钮正常时的颜色 - 灰

按下时的文字颜色:
按下时的 - 白色

以下为步骤:

  • 先找到在values目录下的color.xml(定义的色彩xml,或为colors.xml,若没就创建)文件,在里面加入以下自定义颜色(注意不是用color标签)的代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--button中的文字变换色-->
    <drawable name="button_text_white">#FFFFFF</drawable>
    <drawable name="button_text_gray">#A4A4A4</drawable>

    <!--dialog button 背景色-->
    <color name="button_bg_black">#212121</color>
    <color name="button_bg_gray">#7a7a7a</color>

</resources>
  • 然后在res下新建drawable目录,里面新建selector_btn_click_bg.xml和sel_button_click_text_color.xml文件,代码如下:

    sel_button_click_text_color.xml:

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

    <item android:color="@drawable/button_text_gray" android:state_enabled="true"
        android:state_focused="false"
        android:state_pressed="false" />

    <!--按下时的字体色  - 白色  -->
    <item android:color="@drawable/button_text_white" android:state_pressed="true" />

    <item android:color="@drawable/button_text_gray" android:state_focused="true" />

</selector>

selector_btn_click_bg.xm:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_pressed="true" >
       <shape android:bottomLeftRadius="4dp"
           android:bottomRightRadius="0dp"
           android:topLeftRadius="0dp"
           android:topRightRadius="0dp">
           <corners android:radius="0dp"/>
           <gradient android:angle="90"
               android:startColor="#acacac"
               android:endColor="#acacac"/>
           <stroke android:color="#acacac"
                   android:width="0dp"/>
       </shape>

   </item>

    <!--正常色-->
    <item>
        <shape android:shape="rectangle">
            <corners android:bottomLeftRadius="4dp"
                android:bottomRightRadius="0dp"
                android:topLeftRadius="0dp"
                android:topRightRadius="0dp"/>
            <gradient android:angle="90"
                      android:startColor="#cacaca"
                      android:endColor="#cacaca"/>
            <stroke android:color="#cacaca"
                android:width="0dp" />
        </shape>
    </item>
</selector>
  • 最后,在布局的按钮上,添加statelist drawable修饰:
<!--...省略部分代码...-->
<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_alignParentBottom="true"
            android:gravity="center_vertical">

            <Button
                android:id="@+id/bt_ed_dialog_off"
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:background="@color/button_bg_gray"
                android:text="取消"
                android:textColor="@drawable/sel_button_click_text_color"
                android:textSize="16sp" />

        <!--android:background="@drawable/selector_btn_click_bg" 
        此处没有使用-->
            <Button
                android:id="@+id/bt_ed_dialog_ok"
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:background="@color/button_bg_black"
                android:text="确定"
                android:textColor="@drawable/sel_button_click_text_color"
                android:textSize="16sp" />

        </LinearLayout>

OK,大功告成

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值