我想知道是否可以通过检查 onCreate 语句动态更改 Activity 的背景颜色(如果设置为“红色”或“蓝色”),然后相应地将 Activity 的背景颜色更改为“红色”或“蓝色”。
我在 StackOverflow 上找到了几个看起来对其他人有用的答案,但它们似乎对我不起作用。这是我尝试过的事情的列表:
将 setBackgroundColor() 与 View 结合使用。
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() 结合不同的样式。其工作原理是,我的元素(例如 TextView)的背景更改为“红色”或“蓝色”,而不是整个 Activity 的背景颜色。
<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 元素的背景颜色。
使用 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));
}
}
最佳答案
首先,你的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));
}
}
关于java - 如何使用 Android SDK 在 onCreate 语句中动态更改 Activity 的背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21375463/