【问题标题】:Dynamic Appcompat Theme cause Activity background fail动态 Appcompat 主题导致 Activity 后台失败
【发布时间】:2015-06-22 12:38:15
【问题描述】:

在我的应用程序 (API 21) 中,用户可以在深色和浅色主题之间切换。 问题是当主题更改活动背景不遵循新主题时! 我试过了:

  • 不同的模拟器和设备,同样的问题。
  • 将 androidmanifest.xml 更改为主题
    “@android:style/Theme.Translucent.NoTitleBar”:不适用于
    ActionBarActivity。
  • 创建一个继承自 AppCompat 的假半透明主题:相同
    问题。
  • 强制逐一查看以应用新主题:活动不喜欢。

我制作了一个示例应用程序来模拟问题 一些图片将解释我的意思

这是在 androidmanifest 中设置时默认的 DarkTheme :

这是在 androidmanifest 中设置时的 LightTheme 默认设置:

这是用户从 DarkTheme 切换到 Light Theme 时得到的结果

这是当用户从 LightTheme 切换到 DarkTheme 时得到的结果

这里是代码文件:

androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.examples.testtheme" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/LightTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

main_Activity.xml(布局文件)

<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView android:text="Sample TextView" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large TextView Sample"
        android:textAppearance="?android:textAppearanceLarge"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        style="?android:starStyle"
        android:text="Like it? "/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceMedium"
        android:text="Try Change Theme..."/>


</LinearLayout>

MainActivity.class(java类)

public class MainActivity extends ActionBarActivity {
    static int themeid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.w("oncreate","static value is " + themeid);
        super.setTheme(themeid);
        this.setTheme(themeid);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        if (id == R.id.action_theme_dark) {
            super.setTheme(R.style.DarkTheme);
            themeid = R.style.DarkTheme;
            Log.w("optionmenuselect","Dark ID= " + R.style.DarkTheme);
            this.recreate();
        }
        if (id == R.id.action_theme_light){
            super.setTheme(R.style.LightTheme);
            themeid = R.style.LightTheme;
            Log.w("optionmenuselect","Light ID= " + R.style.LightTheme);
            this.recreate();
        }

        return super.onOptionsItemSelected(item);
    }
}

menu_main.xml

<menu 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" tools:context=".MainActivity">
    <item android:id="@+id/action_settings" android:title="@string/action_settings"
        android:orderInCategory="100" app:showAsAction="never" />
    <item android:id="@+id/action_theme_light" android:title="Light Theme"/>
    <item android:id="@+id/action_theme_dark" android:title="Dark Theme"/>
</menu>

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="LightTheme" parent="Theme.AppCompat.Light" >
        <!-- Customize your theme here. -->
    </style>
    <style name="DarkTheme" parent="Theme.AppCompat">

    </style>

</resources>

【问题讨论】:

    标签: java android android-theme android-appcompat


    【解决方案1】:

    您只需将setTheme(themeid) 移动到super.onCreate(savedInstanceState) 之前。

    【讨论】:

    • 你救了我,谢谢。实际上,在我得到这个简单的绝妙答案之前,我就开始将我的活动从带有 AppCompact 的 ActionBarActiviy 更改为带有 Holo 主题的 Activity。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-29
    • 2015-03-26
    • 1970-01-01
    相关资源
    最近更新 更多