我的 android activity 中没有显示按钮背景颜色

Button background color is not showing in my android activity

我创建了一个计算器 activity 作为更大应用程序的一部分,并为不同的组件创建了主题和样式。然而,当我为“buttonOperator”样式分配橙色背景颜色时,它没有显示在使用这种样式的按钮上的 activity 中,感谢向我解释我做错了什么,也许 how/what 工具可用于解决 android studio 中的此类布局问题,如果此类工具或方法可用的话。

下面是与问题相关的代码:

calculatorActivity.xml

<\?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/light_Black"
    android:orientation="vertical"
    android:theme="@style/Theme.UnitConverterAdvance.Calculator"
    tools:context=".CalculatorActivity">

    <LinearLayout style="@style/buttonRow">

        <Button
            style="@style/buttonNumber"
            android:text="@string/button_7" />

        <Button
            style="@style/buttonNumber"
            android:text="@string/button_8" />

        <Button
            style="@style/buttonNumber"
            android:text="@string/button_9" />

        <Button
            style="@style/buttonOperator"
            android:text="@string/button_division" />
    </LinearLayout>

    <LinearLayout style="@style/buttonRow">

        <Button
            style="@style/buttonNumber"
            android:text="@string/button_0" />

        <Button
            style="@style/buttonNumber"
            android:text="@string/button_dot" />

        <Button
            style="@style/buttonOperator"
            android:text="@string/button_equal" />

        <Button
            style="@style/buttonOperator"
            android:text="@string/button_plus" />
    </LinearLayout>
</LinearLayout>

Style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="buttonRow">
        <item name="android:layout_weight">1</item>
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">0dp</item>
    </style>

    <style name="buttonNumber">
        <item name="android:layout_weight">1</item>
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:background">@null</item>
        <item name="android:textSize">25sp</item>
        <item name="android:textColor">@color/white</item>
    </style>

    <style name="buttonOperator">
        <item name="android:layout_weight">1</item>
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">match_parent</item>       
        <item name="backgroundColor">@color/orange</item>
        <item name="android:textSize">25sp</item>
        <item name="android:textColor">@color/white</item>
    </style>
</resources>

Theme.xml

<resources xmlns:tools="http://schemas.android.com/tools">

<style name="Theme.UnitConverterAdvance" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/purple_500</item>
    <item name="colorPrimaryVariant">@color/purple_700</item>
    <item name="colorOnPrimary">@color/white</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/teal_200</item>
    <item name="colorSecondaryVariant">@color/teal_700</item>
    <item name="colorOnSecondary">@color/black</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
</style>

<style name="Theme.UnitConverterAdvance.Calculator" parent="Theme.AppCompat">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/light_Black</item>
    <item name="colorPrimaryVariant">@color/orange</item>
    <item name="colorOnPrimary">@color/white</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
</style>

</resources>

////尝试在.xml文件中手动给出。它工作正常。

           <Button
            android:id="@+id/btn_proceToPay"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:layout_below="@id/rl3"
            android:layout_marginTop="20dp"
            android:background="@color/primaryBackgroung"
            android:enabled="false"
            android:text="Proceed To Pay"
            android:textAllCaps="false"
            android:textColor="@color/whiteText"
            android:textSize="16sp"
            android:textStyle="bold" />

正如 commonsware 在他的评论中指出的那样,答案是在 Style.xml 中使用 backgroundTint 而不是 backgroundColor。或将 'button' 标签更改为 'android.widget.Button'.

因为自 Android Studio 4.1 以来,布局中的任何 Button 元素都会变成 MaterialButton 小部件,这会忽略背景属性。

有关详细信息,请参阅