我想知道是否可以通过检查onCreate语句动态更改活动的背景颜色,如果设置显示“红色”或“蓝色”,然后相应地将活动的背景颜色更改为'红色'或'蓝色'。
我在StackOverflow上找到了几个看起来像是为其他人工作的答案,但它们似乎对我不起作用。这是我尝试的一系列事项:
将setBackgroundColor()与视图结合使用。
if (settings.getBoolean("Blue", true)) {
View currentView = (View) findViewById(R.layout.home_screen);
currentView.setBackgroundColor(getResources().getColor(R.color.blue));
} if (settings.getBoolean("Red", false)) {
View currentView = (View) findViewById(R.layout.home_screen);
currentView.setBackgroundColor(getResources().getColor(R.color.red));
}
使用setTheme()结合不同的样式。这样做的方式是我的元素背景如TextViews改为“红色”或“蓝色”而不是整个活动的背景颜色。
<style name="RedTheme">
<item name="android:background">@color/redbackground</item>
<item name="android:windowBackground">@color/redbackground</item>
<item name="android:colorBackground">@color/redbackground</item>
</style>
在这种情况下,只有android:background似乎会改变任何东西,但不幸的是它会改变我的TextView元素的backgroundcolor。
使用getWindow()。getDecorView()。setBackgroundColor(Color.BLUE),但这似乎什么都不做。
有没有人可以告诉我我做错了什么或告诉我如何才能成功实现这一目标?
非常感谢提前!
按要求添加完整的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
changeTheme();
setContentView(R.layout.activity_home_screen);
}
private void changeTheme() {
SharedPreferences settings = getSharedPreferences(Settings, 0);
if (settings.getBoolean("Blue", true)) {
View currentView = (View) findViewById(R.layout.activity_home_screen);
currentView.setBackgroundColor(getResources().getColor(R.color.blue));
} if (settings.getBoolean("Red", false)) {
View currentView = (View) findViewById(R.layout.activity_home_screen);
currentView.setBackgroundColor(getResources().getColor(R.color.red));
}
}
答案 0 :(得分:2)
首先,home_screen
的主要布局应该是 FrameLayout ,不要忘记给它一些ID,就像这样
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".HomeScreenActivity"
android:id="@+id/activity_home_screen">
<!-- rest of your UI here -->
</FrameLayout>
然后在您的onCreate
创建View
变量中,通过 ID 将其连接到此布局。
从现在开始,setBackgroundColor()
应该可以正常工作(下面的代码只有少量更改适用于我 - 我不是指设置,但是我会在一段时间后调用changeTheme()
更改值)
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
changeTheme();
}
private void changeTheme()
{
SharedPreferences settings = getSharedPreferences(Settings, 0);
View currentView = (View) findViewById(R.id.activity_home_screen);
if (settings.getBoolean("Blue", true))
{
currentView.setBackgroundColor(getResources().getColor(R.color.blue));
}
else if (settings.getBoolean("Red", false))
{
currentView.setBackgroundColor(getResources().getColor(R.color.red));
}
}
答案 1 :(得分:0)
将其从onCreate
移至单独的方法:
private void changeTheme(){
if (settings.getBoolean("Blue", true)) {
View currentView = (View) findViewById(R.layout.home_screen);
currentView.setBackgroundColor(getResources().getColor(R.color.blue));
} else if (settings.getBoolean("Red", false)) {
View currentView = (View) findViewById(R.layout.home_screen);
currentView.setBackgroundColor(getResources().getColor(R.color.red));
}
}
然后从changeTheme()
或其他
onClick()