[英]How can I modify how a button looks while pressed in Android Studio
我一直在試驗按鈕屬性以及我可以用按鈕做什么。 我想修改按鈕點擊時的外觀。 我創建了三個可繪制資源文件,兩個用於每個按鈕狀態(按下或默認),一個用於狀態列表以傳遞給主活動 xml 中的按鈕。 但是,這不起作用,因為當我單擊它時按鈕保持不變。
custom_button_pressed.xml 文件代碼
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@color/button_color_pressed"/>
<corners android:radius="30dp"/>
</shape>
custom_button.xml 文件(默認狀態文件)代碼:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="30dp"/>
<solid android:color="@color/button_color"/>
</shape>
custom_button_full.xml 文件代碼:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/custom_button_pressed"
android:state_pressed="true" />
<item android:drawable="@drawable/custom_button"/>
</selector>
主 xml 文件中按鈕的代碼片段:
<Button
android:id="@+id/buttonAboutApp"
android:layout_width="160dp"
android:layout_height="105dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:layout_marginBottom="44dp"
android:background="@drawable/custom_button_full"
android:drawableLeft="@mipmap/about_app_icon"
android:text="@string/About_app"
android:textAllCaps="false"
android:textColor="@color/app_background"
app:backgroundTint="@color/button_color"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
我究竟做錯了什么 ? 先感謝您
如果您正在使用一個版本的材質組件庫低於1.2.0, 該android:background
屬性不被支持MaterialButton
。 如果您的應用程序/活動使用MaterialComponents
主題,則任何<Button>
標簽都將自動膨脹為MaterialButton
實例,因此您的自定義背景將不起作用。
由於您無論如何都試圖覆蓋按鈕的外觀,因此您可能只需要一個基本的android.widget.Button
而不是MaterialButton
。 您可以通過在布局文件的視圖標記中指定完全限定名稱來實現此目的:
<android.widget.Button
android:background="@drawable/custom_button_full"
.../>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.