【问题标题】:Change XML Selector from Java Code(not drawable)从 Java 代码更改 XML 选择器(不可绘制)
【发布时间】:2012-11-20 14:50:24
【问题描述】:

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

如何更改 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>

【问题讨论】:

    标签: android xml selector statelistdrawable


    【解决方案1】:

    使用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
    

    这可能会有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多