Android?attr / colorPrimary无法调用setTheme

时间:2016-04-21 10:55:48

标签: android xml android-layout android-theme

我有一个有2个主题的Android应用程序。用户可以从应用程序中其他位置的设置活动切换主题。默认主题为AppThemeBlue,当应用启动时,一切都很顺利。但是,用户将主题从设置活动更改为AppThemeGreen后,不会进行任何更改。 以下是style.xml

中的2个主题
<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));
        }
    }
}

我的?attr/mycolor中有main_layout.xml吗?如果是这样,请解释?attr的问题是什么,因为style.xml两个主题都有相同的项目名称。

任何帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分:1)

如果您的活动主题只设置一次,则在super.onCreate()和setContentView()之前调用setTheme()方法。

如果要在运行时更改主题,即。通过单击按钮,您需要重新启动活动。

阅读this article,了解有关在运行时更改主题的更多信息。