【问题标题】:Android ?attr/colorPrimary not working on call to setThemeAndroid ?attr/colorPrimary 无法调用 setTheme
【发布时间】:2016-04-21 10:55:48
【问题描述】:

我有一个有 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));
        }
    }
}

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

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

【问题讨论】:

  • 尝试在 setContentView 之前重新启动活动并更新主题
  • 链接:mrbool.com/…

标签: android xml android-layout android-theme


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多