1

我正在尝试创建一个通用的自定义组合框库。

如何更改 xml 代码 -> java 代码。

请给我提示。

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:state_pressed="false">
        <shape android:shape="rectangle">            
<padding android:bottom="5dp" android:left="5dp" android:top="5dp" />
            <stroke android:width="1px" android:color="@color/textbox_border_color" />
            <solid android:color="@color/textbox_nomal_color" />
            <corners android:bottomRightRadius="15dp" />
        </shape>
    </item>
    <item android:state_enabled="true" android:state_pressed="true">
        <shape android:shape="rectangle">
            <padding android:bottom="5dp" android:left="5dp" android:top="5dp" />
            <stroke android:width="1px" android:color="@color/textbox_border_color" />
            <solid android:color="@color/textbox_pressed_color" />
            <corners android:bottomRightRadius="15dp" />            
        </shape>
    </item>
    <item android:state_enabled="false"><shape android:shape="rectangle">
            <padding android:bottom="5dp" android:left="5dp" android:top="5dp" />
            <stroke android:width="1px" android:color="@color/textbox_border_color" />
            <solid android:color="@color/textbox_disable_color" />
            <corners android:bottomRightRadius="15dp" />
        </shape>
    </item>
</selector>
4

1 回答 1

0

使用StateListDrawable通过代码查看选择器:

StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
    getResources().getDrawable(R.drawable.pressed));
states.addState(new int[] {android.R.attr.state_focused},
    getResources().getDrawable(R.drawable.focused));
states.addState(new int[] { },
    getResources().getDrawable(R.drawable.normal));

imageView.setImageDrawable(states);  //YOUR IMAGE HERE
//AND FOR BUTTON
 button.setBackgroundDrawable(states);//FOR BUTTON

这可能会有所帮助

于 2015-02-28T19:13:57.140 回答