gpt4 book ai didi

java - 更改主题导致双操作栏故障

转载 作者:行者123 更新时间:2023-12-02 04:17:00 25 4
gpt4 key购买 nike

因此,在我的应用程序中,我创建了一种让用户将主题设置为深色或浅色的方法。我将其实现到每个 Activity 中,但现在如果主题是深色的,则 Activity 有 2 个操作栏。如果我将其保留为浅色主题,则仅显示一个操作栏。这对我来说没有任何意义。非常感谢所有帮助。谢谢。

以下是 Activity 图片:

2 action bars

这是 MainActivity 的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
settings = getSharedPreferences(PREFS_NAME, 0);
checkDark(settings.getBoolean("darkMode", false));
super.onCreate(savedInstanceState);
managerHelper = new ManagerDatabaseAdapter(this);
setContentView(R.layout.activity_main);
adapter = new RVAdapter(this, getData());
RV = (RecyclerView) findViewById(R.id.mainV);
layoutManager = new LinearLayoutManager(this);
RV.setLayoutManager(layoutManager);
RV.setAdapter(adapter);
adapter.setClickListener(this);
}

@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);
// getMenuInflater().inflate(R.menu.menu_main, li)
return true;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && exitD) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Confirm exit")
.setPositiveButton("EXIT", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
}
return super.onKeyDown(keyCode, event);
}

@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
switch (id) {
case R.id.action_settings:
Intent intent = new Intent(this, SettingsActivity.class);
intent.putExtra(EXTRA_MESSAGE, true);
startActivity(intent);
overridePendingTransition(R.anim.abc_popup_enter, R.anim.abc_fade_out);
return true;
case R.id.action_surveyList:
Intent survey = new Intent(this, GetSurvey.class);
survey.putExtra(EXTRA_MESSAGE, true);
startActivity(survey);
overridePendingTransition(R.anim.abc_popup_enter, R.anim.abc_fade_out);
return true;
}
return super.onOptionsItemSelected(item);
}

private void checkDark(boolean isChecked) {
if (isChecked)
setTheme(R.style.Dark);
else
setTheme(R.style.AppTheme);
}
}

这是 styles.xml:

<resources>>

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#E53935</item>
<item name="colorPrimaryDark">#C62828</item>
<item name="android:navigationBarColor">#E53935</item>
<item name="android:colorAccent">#1565C0</item>
<item name="android:colorForeground">@color/foreground_material_light</item>
<item name="android:colorBackground">@color/background_material_light</item>
<!-- Other attributes -->
</style>

<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:navigationBarColor">#E53935</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@color/primary_dark_material_light</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>

<style name="Dark" parent="@android:style/Theme.Material" >
<item name="android:colorForeground">@color/foreground_material_dark</item>
<item name="android:colorPrimary">@color/primary_material_dark</item>
<item name="android:colorPrimaryDark">@color/primary_dark_material_dark</item>
<item name="android:colorAccent">@color/accent_material_dark</item>
<item name="android:navigationBarColor">@color/primary_material_dark</item>
</style>

<style name="AppTheme.NoActionBar.Dark" parent="@style/AppTheme.NoActionBar" >
<item name="colorPrimary">#E53935</item>
<item name="colorPrimaryDark">#C62828</item>
<item name="android:navigationBarColor">#E53935</item>
<item name="android:colorAccent">#1565C0</item>
<item name="android:colorBackground">@color/background_material_dark</item>
<item name="android:textColorPrimary">@android:color/primary_text_dark</item>
<item name="android:textColorPrimaryInverse">@android:color/primary_text_light</item>
<item name="android:textColorSecondary">@android:color/secondary_text_dark</item>
<item name="android:textColorSecondaryInverse">@android:color/secondary_text_light</item>
<item name="android:statusBarColor">@color/primary_dark_material_dark</item>
</style>

</resources>

最佳答案

不确定这是否是问题的原因,但只是需要注意一下。您的 AppTheme 的父级来自 AppCompat,但您的深色主题正在使用内置的 Material 主题。

关于java - 更改主题导致双操作栏故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33200116/

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