[英]How do I set button states of a Floating Action Button using xml?
我在下面的 xml 中有一個浮動操作按鈕,並且可以正常工作
fragment_one.xml
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/btnFloatMain"
android:layout_width="80dp"
android:layout_height="80dp"
android:clickable="true"
android:layout_gravity="bottom|right"
android:layout_marginBottom="15dp"
android:layout_marginRight="15dp"
app:maxImageSize="80dp"
app:fabCustomSize="80dp"
app:srcCompat="@drawable/button_float_main"
/>
但是我想在按下時設置浮動操作按鈕的按鈕狀態,所以我在下面創建了 xml
button_states_float_main.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
app:maxImageSize="80dp"
app:fabCustomSize="80dp"
app:srcCompat="@drawable/button_float_main_pressed"/>
<item
app:maxImageSize="80dp"
app:fabCustomSize="80dp"
app:srcCompat="@drawable/button_float_main"/>
</selector>
並將我的按鈕 xml 修改為
fragment_one.xml
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/btnFloatMain"
android:layout_width="80dp"
android:layout_height="80dp"
android:clickable="true"
android:layout_gravity="bottom|right"
android:layout_marginBottom="15dp"
android:layout_marginRight="15dp"
app:srcCompat="@drawable/button_states_float_main"
/>
但我認為我的設置方式不正確,因為我的應用程序現在崩潰了。
利用
android:drawable="@drawable/button_float_main_pressed"
代替
app:srcCompat="@drawable/button_float_main_pressed"
在選擇器中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.