如何在android中获得带有选择器drawable的背景按钮?

时间:2014-11-25 11:57:59

标签: android drawable

如何在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>

1 个答案:

答案 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);