将一些按钮的背景设置为可绘制的 custom_button.xml。选择器应该在按下、禁用和默认时更改可绘制对象。出于某种原因,当我按下 View 中的任何按钮时,布局中的最后一个按钮也将其可绘制对象设置为 rounded_corner_pressed。这都是通过选择器完成的。我试过添加 state_focus 无济于事。有任何想法吗?
编辑 试图使问题更清楚
自定义按钮.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true" f
android:drawable="@drawable/rounded_corner_pressed" />
<item
android:state_enabled="false"
android:drawable="@drawable/rounded_corner_disabled" />
<item
android:drawable="@drawable/rounded_corner" />
</selector>
rounded_corner.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/white"/>
<corners android:topLeftRadius="8dp"
android:topRightRadius="8dp"
android:bottomLeftRadius="8dp"
android:bottomRightRadius="8dp"/>
<stroke
android:width="1dp"
android:color="@color/background_color" />
<padding android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
rounded_corner_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="@android:color/holo_green_light"/>
<corners android:topLeftRadius="8dp"
android:topRightRadius="8dp"
android:bottomLeftRadius="8dp"
android:bottomRightRadius="8dp"/>
<stroke
android:width="1dp"
android:color="@color/background_color" />
<padding android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
布局在
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="10sp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/questionView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_corner"
android:gravity="center_horizontal|center_vertical"
android:minHeight="85dp"
android:text="Question"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/black"
android:textStyle="bold" />
<Button
android:id="@+id/answerOne"
android:layout_width="match_parent"
android:layout_height="65dip"
android:layout_marginTop="8dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/custom_button"
android:layout_below="@id/questionView"
android:text="Answer 1" />
<Button
android:id="@+id/answerTwo"
android:layout_width="match_parent"
android:layout_height="65dip"
android:layout_marginTop="8dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/custom_button"
android:layout_below="@id/answerOne"
android:text="Answer 2" />
<Button
android:id="@+id/answerThree"
android:layout_width="match_parent"
android:layout_height="65dip"
android:layout_marginTop="8dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/custom_button"
android:layout_below="@id/answerTwo"
android:text="Answer 3" />
<Button
android:id="@+id/answerFour"
android:layout_width="match_parent"
android:layout_height="65dip"
android:layout_marginTop="8dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/custom_button"
android:layout_below="@id/answerThree"
android:text="Answer 4" />
<ProgressBar
android:id="@+id/retrievingProgress"
style="?android:attr/progressBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:indeterminate="true"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
最佳答案
您可以简化您的布局。
对于您的按钮,只需使用 MaterialButton
在Material Components library .
类似于:
<com.google.android.material.button.MaterialButton
android:text="BUTTON"
app:cornerRadius="8dp"
app:strokeWidth="1dp"
app:strokeColor="@color/myColor"
android:backgroundTint="@color/myselector"
../>
对于 TextView,您可以使用 TextInputLayout
.
类似于:
<com.google.android.material.textfield.TextInputLayout
app:hintEnabled="false"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
app:boxCornerRadiusBottomStart="8dp"
app:boxCornerRadiusBottomEnd="8dp"
app:boxCornerRadiusTopEnd="8dp"
app:boxCornerRadiusTopStart="8dp"
...>
<com.google.android.material.textfield.TextInputEditText
android:text="Text"
.../>
</com.google.android.material.textfield.TextInputLayout>
关于android - 按钮状态可绘制按下触发多个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57641527/