如何以编程方式将活动的背景颜色设置为白色?如何以编程方式将活动的背景颜色设置为白色?
105
A
回答
119
得到一个处理使用的根布局,然后设置背景色。根布局就是你所谓的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));
56
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:id="@+id/myScreen"
</LinearLayout>
换句话说,“android:background”是您想要更改的XML中的标记。
如果您需要动态更新的背景值,请参阅以下内容:
64
我喜欢着色由主题
<style name="CustomTheme" parent="android:Theme.Light">
<item name="android:windowBackground">@color/custom_theme_color</item>
<item name="android:colorBackground">@color/custom_theme_color</item>
</style>
196
加入这一行中的活动,之后setContentView()
呼叫
getWindow().getDecorView().setBackgroundColor(Color.WHITE);
7
你可以用它来打电话预定义的android颜色:
element.setBackgroundColor(android.R.color.red);
如果您想使用自己的自定义颜色之一,则可以将自定义颜色添加到strings.xml,然后使用下面的代码调用它。
element.setBackgroundColor(R.color.mycolour);
但是,如果你想在你的layout.xml中设置颜色,你可以修改并添加下面的任何元素来接受它。所以
View root = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
,改变颜色为白色:
android:background="#FFFFFF"
3
要获得在XML文件中定义的根认为,如果不采取行动吧,你可以使用这个
root.setBackgroundResource(Color.WHITE);
3
View randview = new View(getBaseContext());
randview = (View)findViewById(R.id.container);
randview.setBackgroundColor(Color.BLUE);
为我工作。谢谢。
1
final View rootView = findViewById(android.R.id.content);
rootView.setBackgroundResource(...);
6
在你onCreate()
方法:
getWindow().getDecorView().setBackgroundColor(getResources().getColor(R.color.main_activity_background_color));
你也需要添加到文件夹的值称为color.xml
一个新的XML文件,并分配有一个新的颜色属性:
color.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="main_activity_background_color">#000000</color>
</resources>
请注意,您可以命名为color.xml
任何你想要的名字,但你通过代码引用它为R.color.yourId
。
编辑
因为getResources().getColor()
已过时,使用getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.main_activity_background_color));
代替。
1
Button btn;
View root;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
root =findViewById(R.id.activity_main).getRootView();
root.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
});
}
相关问题
- 1. 如何以编程方式设置UINavigationbar的背景颜色?
- 2. 如何以编程方式设置UILabel的背景颜色?
- 3. 如何以编程方式设置tablelayout行背景颜色
- 4. 如何将背景颜色设置为白色
- 5. 以编程方式设置形状的背景颜色
- 6. 以编程方式设置listview的背景颜色
- 7. 设置活动的背景颜色
- 8. 以编程方式将背景色调设置为EditText视图
- 9. Silverlight:以编程方式设置组合框背景颜色
- 10. 活动时设置背景颜色
- 11. 以编程方式将节点图像的背景设置为颜色
- 12. NSProgressIndicator无法将背景颜色设置为白色
- 13. 如何以编程方式为线性布局设置背景颜色?
- 14. 如何以编程方式为数据网格行设置背景颜色
- 15. $。将背景颜色设置为当前设置的颜色
- 16. 如何以编程方式设置WatchKit应用程序的背景颜色?
- 17. Pascal - 将背景设置为白色(不是灰色,纯白色)
- 18. 将背景颜色设置为色调颜色
- 19. Textfield背景色白色,活动灰色背景色
- 20. 如何为NSTableCellView设置背景颜色?
- 21. 如何为BottomBar设置背景颜色?
- 22. 如何以编程方式更改按钮的背景颜色
- 23. 背景颜色仍然是白色,无论设置如何
- 24. 如何在Gnome 3中以编程方式将背景设置为纯色?
- 25. 如何将viewcontroller的背景颜色从灰色更改为设置的颜色?
- 26. 如何以编程方式围绕角落并设置随机背景颜色
- 27. 将背景颜色设置为progressBar
- 28. 将背景颜色设置为DotNet.Highcharts
- 29. 将背景设置为渐变颜色
- 30. 将背景颜色设置为页面
当我这样做时,Eclipse将其标记为“应该在这里通过解析颜色而不是资源ID:getResources()。getColor(android.R.color.red)”。 – joriki 2013-08-06 13:03:25