android:修改button背景色无效

一,问题:

xml:

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="200px">
        <Button
            android:id="@+id/btn1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:background="#ff0000"
            android:text="按钮一"/>
        <Button
            android:id="@+id/btn2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="按钮二"/>
        <Button
            android:id="@+id/btn3"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="按钮三"/>
    </LinearLayout>

注意,btn1加了:android:background="#ff0000"

但并未起作用,如图,相反,形状还发生了变化

二,解决:

编辑res/values/themes.xml,如图:

把parent的值修改为: Theme.MaterialComponents.DayNight.NoActionBar.Bridge

Theme.MaterialComponents.DayNight.NoActionBar.Bridge

测试效果:

 

posted @ 2025-05-01 09:26  刘宏缔的架构森林  阅读(58)  评论(0)    收藏  举报