我有一个有 2 个主题的安卓应用程序。用户可以从应用中其他位置的设置活动切换主题。默认主题为AppThemeBlue
,当应用程序启动时,一切都很好。但是,在用户将主题从设置活动更改为AppThemeGreen
后,不会进行任何更改。以下是我在style.xml
中的两个主题
<resources>
<style name="AppThemeBlue" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/BlueColorPrimary</item>
<item name="colorPrimaryDark">@color/BlueColorPrimaryDark</item>
<item name="colorAccent">@color/BlueColorPrimaryDark</item>
</style>`
<style name="AppThemeGreen" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/GreenColorPrimary</item>
<item name="colorPrimaryDark">@color/GreenColorPrimaryDark</item>
<item name="colorAccent">@color/GreenColorPrimaryDark</item>
</style>
</resources>
我的main_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppThemeBlue.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
android:titleTextColor="?attr/colorPrimary"
app:popupTheme="@style/AppThemeBlue.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:id="@+id/content_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/colorPrimary"
android:orientation="vertical" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:background="?attr/colorPrimaryDark"
android:src="@android:drawable/ic_input_add" />
</LinearLayout>
和我在ActivityMain
中的代码
public class ActivityMain extends AppCompatActivity {
protected static FloatingActionButton mFloatingActionButton;
protected static Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mMainContentLayout = (LinearLayout) findViewById(R.id.content_bg);
mNavigationView = (NavigationView) findViewById(R.id.nav_view);
mFloatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
setTheme();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
setTheme();
}
public void setTheme() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(ActivityMain.this);
String color = sp.getString(getResources().getString(R.string.themes_key), null);
if (color != null) {
switch (color) {
case ConstentValues.COLORBLUE: {
setStausBar(R.color.BlueColorPrimaryDarkStatus);
setTheme(R.style.AppThemeBlue);
break;
}
case ConstentValues.COLORGREEN: {
setStausBar(R.color.GreenColorPrimaryDarkStatus);
setTheme(R.style.AppThemeGreen);
break;
}
}
} else {
setStausBar(R.color.BlueColorPrimaryDarkStatus);
setTheme(R.style.AppThemeBlue);
}
}
private void setStausBar(int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = ActivityMain.this.getWindow();
window.setStatusBarColor(ActivityMain.this.getResources().getColor(color));
}
}
}
是不是我main_layout.xml
里有?attr/mycolor
?如果是这样,请解释 ?attr 有什么问题,因为在style.xml
中两个主题具有相同的项目名称。
任何帮助将不胜感激。 谢谢。
如果你的活动主题只设置一次,那么在super.onCreate()和setContentView()之前调用setTheme()方法。
如果要在运行时更改主题,即通过单击按钮,则需要重新启动活动。
有关在运行时更改主题的详细信息,请阅读本文。