我使用选择器和形状定义了按钮样式,并将其保存在drawable文件夹中。然后将其与布局文件中的按钮相关联。
当按下一个按钮时,按钮背景颜色已更改,但松开按钮后仍显示默认颜色。我希望选定的颜色在按钮单击后仍然保持不变。我需要帮助。提前感谢。
在drawable / highscore_button_style.xml中的按钮样式
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" >
<shape android:shape="rectangle" >
<solid android:color="@color/high_score_btn_bg_selected" />
<corners android:radius="5dp" />
<stroke android:color="@color/high_score_btn_bg_selected" android:width="1dp" />
</shape>
</item>
<item android:state_pressed="true" >
<shape android:shape="rectangle" >
<solid android:color="@color/high_score_btn_bg_selected" />
<corners android:radius="5dp" />
<stroke android:color="@color/high_score_btn_bg_selected" android:width="1dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle" >
<solid android:color="@color/high_score_btn_bg" />
<corners android:radius="5dp" />
<stroke android:color="@color/high_score_btn_bg" android:width="1dp" />
</shape>
</item>
</selector>
在布局文件中添加按钮:
<Button
android:id="@+id/btnLevel1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/highscore_button_style"
android:layout_marginRight="1dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:layout_weight="1"
android:text="1"
android:textColor="@color/high_score_btn_text" />
<Button
android:id="@+id/btnLevel2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/highscore_button_style"
android:layout_marginRight="1dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:layout_weight="1"
android:text="2"
android:textColor="@color/high_score_btn_text" />
<Button
android:id="@+id/btnLevel3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/highscore_button_style"
android:layout_marginRight="1dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:layout_weight="1"
android:text="3"
android:textColor="@color/high_score_btn_text" />