Android 在应用程序运行时更改XML形状颜色
在应用程序运行时,是否可以在Java代码中更改矩形(以xml绘制)颜色 My rectangle.xml:Android 在应用程序运行时更改XML形状颜色,android,xml,Android,Xml,在应用程序运行时,是否可以在Java代码中更改矩形(以xml绘制)颜色 My rectangle.xml: <?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape"> <stroke android:width="2dp"
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape">
<stroke android:width="2dp" android:color="#ffffff" />
<padding android:left="20dp"
android:top="20dp"
android:right="20dp"
android:bottom="20dp" />
<solid android:color="#006600" />
</shape>
只有当我将它放在OnCreate方法中时,它才起作用
我想通过一个按钮来改变rect颜色,所以我把这段代码放在按钮的onClick()方法中。但当我在应用程序运行时点击按钮时,颜色不会改变。有什么建议吗?您可以将此代码放在一个单独的方法中,您可以通过单击按钮调用该方法< P>使用此代码,它工作了,或者可以考虑使用VIELTRADIORK.FALIDIDATE()重新绘制视图矩形,但是它不应该是NASBAGE:
View viewrectangle;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
viewrectangle = (View) findViewById(R.id.myRectangleView);
}
public void doClick(View v) {
GradientDrawable sd = (GradientDrawable) viewrectangle.getBackground();
sd.setColor(0xffffff00);
sd.invalidateSelf();
}
在本例中,main.xml中设置了“doClick()”方法:
<Button android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Button"
android:onClick="doClick"/>
您可以尝试使用颜色过滤器。我以前用过它来改变按钮的颜色(注意,它们一开始是标准灰色),如果你用另一种颜色开始,结果可能会非常不同。总之,我是如何做到这一点的: 导入PorterDuff图形资料:
import android.graphics.PorterDuff;
在类中,定义要对其进行颜色过滤的项目并设置过滤器:
Button atdButton = (Button) convertView.findViewById(R.id.attendbutton);
if (atdState[position].equals("P")) {
atdButton.getBackground().setColorFilter(0xFF00FF00, // Set filter to green
PorterDuff.Mode.MULTIPLY);
} else if (atdState[position].equals("T")) {
atdButton.getBackground().setColorFilter(0xFFFFFF00, // Set filter to yellow
PorterDuff.Mode.MULTIPLY);
} else if (atdState[position].equals("E")) {
atdButton.getBackground().setColorFilter(0xFFFF6600, // Set filter to orange
PorterDuff.Mode.MULTIPLY);
} else if (atdState[position].equals("U")) {
atdButton.getBackground().setColorFilter(0xFFFF0000, // Set filter to red
PorterDuff.Mode.MULTIPLY);
} else {
atdButton.getBackground().clearColorFilter();
}
如果下面的任何答案解决了您的问题,或者帮助您解决了问题,您应该单击该答案旁边的复选标记,将其标记为已接受的答案,并给予作者评分。
import android.graphics.PorterDuff;
Button atdButton = (Button) convertView.findViewById(R.id.attendbutton);
if (atdState[position].equals("P")) {
atdButton.getBackground().setColorFilter(0xFF00FF00, // Set filter to green
PorterDuff.Mode.MULTIPLY);
} else if (atdState[position].equals("T")) {
atdButton.getBackground().setColorFilter(0xFFFFFF00, // Set filter to yellow
PorterDuff.Mode.MULTIPLY);
} else if (atdState[position].equals("E")) {
atdButton.getBackground().setColorFilter(0xFFFF6600, // Set filter to orange
PorterDuff.Mode.MULTIPLY);
} else if (atdState[position].equals("U")) {
atdButton.getBackground().setColorFilter(0xFFFF0000, // Set filter to red
PorterDuff.Mode.MULTIPLY);
} else {
atdButton.getBackground().clearColorFilter();
}