我有一個按鈕有兩種狀態,其中它具有不同的顏色,但顏色不會改變,因為按鈕的顏色是由主題自動設定的。如何避免主題設定的顏色?
按鈕代碼:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@color/purple_200">
<LinearLayout
android:id="@ id/startLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:padding="10dp"
android:orientation="horizontal">
<Button
android:id="@ id/add_note"
android:layout_width="0dp"
android:layout_height="65dp"
android:layout_weight="1"
android:textSize="23sp"
android:background="@drawable/button_states"
android:text="@string/add_button" />
</LinearLayout>
</RelativeLayout>
國家代碼:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/white"/>
<stroke android:color="@color/purple_200" android:width="1dp"/>
</shape>
</item>
<item android:state_pressed="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/purple_200"/>
<stroke android:color="@color/purple_500" android:width="1dp"/>
</shape>
</item>
</selector>
主題代碼:
<resources>
<style name="Theme.NotifyMe" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
</style>
</resources>
uj5u.com熱心網友回復:
要么使用 androidx 庫androidx.appcompat.widget.AppCompatButton 中的按鈕,要么backgroundTintMode將 xml 中按鈕的屬性設定為可繪制物件。
理想情況下,您想使用背景屬性,因此最好使用它
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="64dp"
android:text="Button"
android:background="@drawable/button_states"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="106dp" />
uj5u.com熱心網友回復:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@color/purple_200">
<LinearLayout
android:id="@ id/startLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:padding="10dp"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatButton
android:id="@ id/add_note"
android:layout_width="0dp"
android:layout_height="65dp"
android:layout_weight="1"
android:textSize="23sp"
android:background="@drawable/button_states"
android:text="Add Button" />
</LinearLayout>
</RelativeLayout>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/364910.html
上一篇:等待所有設備上線->等待設備時出錯:等待模擬器上線300秒后超時
下一篇:我該如何解決androidx.appcompat.widget.SearchView無法強制轉換為android.widget.SearchView
