我創建了一個計算器活動作為更大應用程式的一部分,并為不同的組件創建了主題和樣式。然而,當我將橙色背景色分配給“buttonOperator”樣式時,它不會顯示在使用這種樣式的按鈕的活動中,感謝向我解釋我做錯了什么以及如何/可以使用哪些工具如果此類工具或方法可用,則在 android studio 中解決此類布局問題。
以下是與該問題相關的代碼:
計算器Activity.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>
樣式.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>
主題.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>
uj5u.com熱心網友回復:
嘗試在drawable檔案夾中創建新布局:drawable>new>new resource file 然后創建自己的布局。這是一個例子
<corners android:radius="20dp"/>
<solid android:color="@color/purple_700"/>
最后將此行與您的布局添加到按鈕:
android:background="@drawable/yourstyle"
uj5u.com熱心網友回復:
////嘗試在.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" />
uj5u.com熱心網友回復:
正如commonsware在他的評論中所指出的,答案是在Style.xml 中使用backgroundTint而不是backgroundColor。或將“按鈕”標簽更改為“android.widget.Button”。
因為從 Android Studio 4.1 開始Button,布局中的任何元素都會變成MaterialButton小部件,從而忽略背景屬性。
有關更多詳細資訊,請參閱commonsware answer
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/460527.html
上一篇:如何按活動添加TabItem?
