我使用 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)
}
当然这段代码没有任何意义,我只是为了让你明白我想做什么而编造的