我在我的Android应用程序中的/res/values/colors.xml下创建了一个colors.xml文件。内容是..。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="Green">#00ff00</color>
</resources>我尝试更新我的一个TableRow的背景使用...
TableRow test = (TableRow)findViewById(R.id.tableRow2);
test.setBackgroundColor(R.color.Green);这不会将其设置为绿色,而是灰色。无论我在colors.xml文件中添加什么值,它始终是相同的灰色。但是这是可行的..。
TableRow test = (TableRow)findViewById(R.id.tableRow2);
test.setBackgroundColor(android.graphics.Color.GREEN);我的colors.xml有问题吗?
发布于 2011-07-10 21:12:55
您应该改用下面的代码:
TableRow test = (TableRow)findViewById(R.id.tableRow2);
test.setBackgroundColor(getResources().getColor(R.color.Green));不幸的是,资源ID和颜色具有相同的类型:int。您应该通过getColor()从资源中获取颜色值,并将该值用作颜色。当您使用资源ID作为颜色时。
发布于 2011-07-10 20:58:08
请尝试使用命令setBackgroundResource,即
TableRow test = (TableRow)findViewById(R.id.tableRow2);
test.setBackgroundResource(R.color.Green);https://stackoverflow.com/questions/6641210
复制相似问题