所以在我的应用程序中,我为用户创建了一种将主题设置为暗或亮的方法。我将它实现到每个活动中,但是现在如果主题是黑暗的,则活动有2个操作栏。如果我将其保留为灯光主题,则只显示一个操作栏。这对我没有任何意义。非常感谢所有帮助。感谢。
以下是活动的图片:
以下是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>
答案 0 :(得分:1)
不确定这是否是问题的原因,但需要注意。您的AppTheme的父级来自AppCompat,但您的黑暗主题是使用内置材质主题。
答案 1 :(得分:1)
将父样式更改为Theme.Material.NoActionBar
:
您的自定义样式:
<style name="Dark" parent="@android:style/Theme.Material.NoActionBar" >
...
</style>