【发布时间】:2019-12-29 16:16:36
【问题描述】:
将一些按钮的背景设置为可绘制的 custom_button.xml。选择器应该在按下、禁用和默认时更改可绘制对象。出于某种原因,当我按下视图中的任何按钮时,布局中的最后一个按钮也将其可绘制设置为 rounded_corner_pressed。这一切都是通过选择器完成的。我试过添加 state_focus 无济于事。有任何想法吗?
编辑试图让问题更清楚
custom_button.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>
【问题讨论】:
标签: android android-layout material-design android-drawable material-components-android