我正在使用新的Android设计导航抽屉。我想在抽屉里添加一个开关。有没有实现这个?
这是菜单xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="@+id/nav_view_menu_group_1"
android:checkableBehavior="single">
<item
android:id="@+id/nav_view_menu_item_myschedule"
android:icon="@drawable/ic_navview_my_schedule"
android:title="@string/navview_menu_item_myschedule"
android:titleCondensed="@string/navview_menu_item_myschedule" />
<item
android:id="@+id/nav_view_menu_item_iolive"
android:icon="@drawable/ic_navview_play_circle_fill"
android:title="@string/navview_menu_item_iolive"
android:titleCondensed="@string/navview_menu_item_iolive"
android:visible="false"/>
<item
android:id="@+id/nav_view_menu_item_explore"
android:icon="@drawable/ic_navview_explore"
android:title="@string/navview_menu_item_explore"
android:titleCondensed="@string/navview_menu_item_explore" />
<item
android:id="@+id/nav_view_menu_item_map"
android:icon="@drawable/ic_navview_map"
android:title="@string/navview_menu_item_map"
android:titleCondensed="@string/navview_menu_item_map"
android:visible="false"/>
</group>
</menu>
如何更改一个
<item>
进行切换:
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Switch"
android:id="@+id/switch1"
android:layout_gravity="center_horizontal" />
我目前正在使用默认布局:
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/activity_main_drawer" />
就像Android通知下的此图片http://i.stack.imgur.com/M9nD7.png
我真的感谢任何反馈。谢谢。
答案 0 :(得分:4)
我发现这样做的一种方法是在你想要的menuItem上使用setActionView
:
mNavigationView.getMenu().findItem(R.id.nav_connect)
.setActionView(new Switch(this));
// To set whether switch is on/off use:
((Switch) mNavigationView.getMenu().findItem(R.id.nav_connect).getActionView()).setChecked(true);
可能还需要一个点击监听器来更改Switch的状态:
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_connect:
((Switch) menuItem.getActionView()).toggle();
return true;
}
}
}
我还没有尝试,但您可以在xml中使用android:actionLayout="@layout/switch_layout"
并指向您创建的自定义布局。
也可以尝试使用可能提供更强大功能的ActionProvider。尽管如此,我还没有尝试过这种方法。
答案 1 :(得分:1)
我们可以通过以下方式实现
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
//Get a reference to your item by id
MenuItem item = menu.findItem(R.id.menu_pick_color);
//Here, you get access to the view of your item, in this case, the layout of the item has a FrameLayout as root view but you can change it to whatever you use
FrameLayout rootView = (FrameLayout)item.getActionView();
//Then you access to your control by finding it in the rootView
YourControlClass control = (YourControlClass) rootView.findViewById(R.id.control_id);
//And from here you can do whatever you want with your control
return true;
}
答案 2 :(得分:0)
如果您只想检查交换机中的更改,则只能像这样设置onCheckedChangeListener
((Switch) navigationView.getMenu().findItem(R.id.darkModeSwitch).getActionView())
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
Toast.makeText(MainActivity.this, "Checked", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Unchecked", Toast.LENGTH_SHORT).show();
}
}
});
如果您想变酷,请使用lambda
((Switch) navigationView.getMenu().findItem(R.id.darkModeSwitch).getActionView())
.setOnCheckedChangeListener((buttonView, isChecked) -> {
if(isChecked) {
Toast.makeText(MainActivity.this, "Checked", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Unchecked", Toast.LENGTH_SHORT).show();
}
});