Android:如何在 Java 中获取 Activity 的背景颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3656586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:52:45 来源:igfitidea点击:
Android: How to get background color of Activity in Java?
提问by TN.
How can I get background color and text color (default for child views) of an Activity in Java?
如何在 Java 中获取 Activity 的背景颜色和文本颜色(子视图的默认颜色)?
回答by zavis
TypedArray array = getTheme().obtainStyledAttributes(new int[] {
android.R.attr.colorBackground,
android.R.attr.textColorPrimary,
});
int backgroundColor = array.getColor(0, 0xFF00FF);
int textColor = array.getColor(1, 0xFF00FF);
array.recycle();
回答by Javier
For Background
背景
TypedArray array = context.getTheme().obtainStyledAttributes(new int[] {
android.R.attr.windowBackground});
int backgroundColor = array.getColor(0, 0xFF00FF);
array.recycle();
view.setBackgroundColor(backgroundColor);