安卓10 Button颜色无法改变

起因

第二个小项目,将安卓版本升到10,结果在自定义Button点击效果时,自定义的颜色根本没有显示,看了很多帖子,都没有用,最后发现是安卓版本的问题。

问题代码

先上代码:
自定义的xml文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <!--Image to pressed display-->
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="@color/teal_700"/>
            <corners android:radius="35dp"/>
        </shape>
    </item>

    <!--Image to default display-->
    <item android:state_pressed="false">
        <shape android:shape="rectangle">
            <solid android:color="#000000"/>
            <corners android:radius="15dp"/>
        </shape>
    </item>
</selector>

button控件

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

    <Button
        android:id="@+id/Btn_Play"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_above="@+id/btn_music"
        android:layout_centerHorizontal="true"
        android:clickable="true"
        android:background="@drawable/play_btn"
        android:onClick="OnPlay"
        />
        ...
</RelativeLayout>
解决过程

在尝试过比如:

  • 调换state_xxx与color属性的位置
  • 将默认的状态放到各个item的最后

等各种不同的方法之后,终于找到了能解决问题的办法

res/values/themes.xml中的Theme.MaterialComponents.DayNight.DarkActionBar
改为Theme.MaterialComponents.DayNight.Bridge即可
----原帖链接----

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.MyProgram" parent="Theme.MaterialComponents.DayNight.Bridge">
        ...
    </style>
</resources>
进阶资料

另附—进阶教程—

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值