如何以编程方式将“活动”的背景色设置为白色?
获取所用根布局的句柄,然后在其上设置背景颜色。根布局就是你使用setContentView调用的内容。
setContentView(R.layout.main);
// Now get a handle to any View contained
// within the main layout you are using
View someView = findViewById(R.id.randomViewInMainLayout);
// Find the root view
View root = someView.getRootView();
// Set the color
root.setBackgroundColor(getResources().getColor(android.R.color.red));
当我这样做时,Eclipse将其标记为“应在此处传递解析的颜色而不是资源ID:getResources()。getColor(android.R.color.red)”。
将最后一行更改为
root.setBackgroundColor(getResources().getColor(android.R.color.red));
这个答案有效;但是根据发问者的说法,它仍然不是完全程序化的。我会在下面建议Arunkumar的答案。
我认为这种选择是不正确的。这不是为活动设置颜色,这会导致透支。正确的答案在下面,应该像
window.decorView.setBackgroundColor(getResolvedColor(R.color.your_color))