[英]How can I make a switch in navigation drawer have functionality?
I'm trying to make a navigation drawer switch which switches between a normal theme and a dark theme of an app, however I can't get the switch to work. 我正在尝试制作一个导航抽屉开关,它可以在应用程序的正常主题和黑暗主题之间切换,但是我无法让开关工作。
I already have a working switch on mainactivity but I can't get it to work in the navigation drawer. 我已经有一个关于mainactivity的工作开关,但我不能让它在导航抽屉中工作。
This is the code to switch between light/dark mode. 这是在亮/暗模式之间切换的代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
if(AppCompatDelegate.getDefaultNightMode()==AppCompatDelegate.MODE_NIGHT_YES) {
setTheme(R.style.HROTheme);
}
else setTheme(R.style.AppTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
modeswitch=(Switch)findViewById(R.id.switch2);
if (AppCompatDelegate.getDefaultNightMode()==AppCompatDelegate.MODE_NIGHT_YES) {
modeswitch.setChecked(true);
}
modeswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
recreate();
}
else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
recreate();
}
}
});
You can try to add an item in values/attrs like this: <attr name="bottomback" format="color" />
您可以尝试在值/ attrs中添加项目,如下所示: <attr name="bottomback" format="color" />
Then define "bottomback" in styles for your both dark and light styles like this: 然后在样式中为你的暗色和浅色样式定义“bottomback”,如下所示:
<item name="bottomback">#000</item>
That must be different in both styles. 两种风格都必须有所不同。 now you can set the backgroundTint of drawer as this: android:backgroundTint="?attr/bottomback"
现在您可以将抽屉的backgroundTint设置为: android:backgroundTint="?attr/bottomback"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.