2

我使用 xml 文件为按钮着色。在 xml 中有这个代码来定义我的按钮被点击时的颜色。

    <?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" >
        <shape>
            <gradient
                  android:startColor="@color/mainRaddoppiaButtonBackgroundEnd"
                android:endColor="@color/mainRaddoppiaButtonBackgroundStart"

                android:angle="270" />
            <stroke
                android:width="0dp"
                android:color="@color/mainRaddoppiaButtonBackgroundStart" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item android:state_focused="true" >
           <shape>
            <gradient
                  android:startColor="@color/mainRaddoppiaButtonBackgroundEnd"
                android:endColor="@color/mainRaddoppiaButtonBackgroundStart"

                android:angle="270" />
            <stroke
                android:width="0dp"
                android:color="@color/mainRaddoppiaButtonBackgroundStart" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item>        
        <shape>
            <gradient
                android:startColor="@color/mainRaddoppiaButtonBackgroundStart"
                android:endColor="@color/mainRaddoppiaButtonBackgroundEnd"
                android:angle="270" />
            <stroke
                android:width="0dp"
                android:color="@color/mainRaddoppiaButtonBackgroundStart" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>

是否可以从 java 中按住我的按钮并使用在我的 xml 文件中设置的“state_pressed”值对其进行着色?

有点像:

  public void onClick(View button) {
     button.seLayout(R.xml.xmlFileName.state_pressed)
}

当然这段代码没有任何意义,我只是为了让你明白我想做什么而编造的

4

3 回答 3

4

尝试这个。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_pressed"
      android:state_pressed="true"/>
<item android:drawable="@drawable/btn_normal" />
</selector>
于 2013-01-18T10:57:08.663 回答
2

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

 button.setBackgroundDrawable(states);//FOR BUTTON
于 2013-01-18T11:19:57.433 回答
0

用这个:

StateListDrawable states = new StateListDrawable(){
    @Override
    protected boolean onStateChange(int[] stateSet) {
        //
    }
};
itemView.setBackground(states);
于 2017-06-20T13:05:32.143 回答