如何在drawable文件夹中使用选择器获取背景按钮?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/btn_press" android:state_pressed="true"></item>
<item android:drawable="@drawable/btn_mati" android:state_selected="false"></item>
</selector>
答案 0 :(得分:-1)
试试这样, 在drawable文件夹中创建button_selector.xml,
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_hover" android:state_pressed="true"></item>
<item android:drawable="@drawable/btn_hover" android:state_focused="true"></item>
<item android:drawable="@drawable/btn_normal" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
<item android:drawable="@drawable/btn_hover" android:state_enabled="false"></item>
</selector>
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button_selector"
/>
在你的java类中,
btn.setBackgroundResource(R.drawable.button_selector);