gpt4 book ai didi

java - Dynamic Appcompat Theme 导致 Activity 背景失败

转载 作者:行者123 更新时间:2023-11-29 05:08:14 25 4
gpt4 key购买 nike

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

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

我做了一个示例App来模拟这个问题 一些图片会解释我的意思

这是在 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);
}
}

菜单主.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>

样式.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>

最佳答案

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

关于java - Dynamic Appcompat Theme 导致 Activity 背景失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29669076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com