Mar*_*ski 10 android android-support-library navigation-drawer android-design-library androiddesignsupport
我需要把Switch放在导航抽屉里面.我正在使用新的设计支持库,但我找不到它是否是可能的.使用时
android:checkable
Run Code Online (Sandbox Code Playgroud)
item只是完全选中,这不是我想要的.
这是我真正想要的截图.这有可能实现吗?

len*_*ooh 24
导航抽屉的菜单项:
<item
android:id="@+id/nav_item1"
android:icon="@drawable/ic_item1"
android:title="item1"
app:actionLayout="@layout/layout_switch"
/>
Run Code Online (Sandbox Code Playgroud)
以及该项目的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.SwitchCompat
android:id="@+id/drawer_switch"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text=""/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
bmh*_*cho 13
我发现这样做的一种方法是在你想要的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);
Run Code Online (Sandbox Code Playgroud)
可能还需要一个点击监听器来改变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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我没试过,但你可以在xml中使用android:actionLayout ="@ layout/switch_layout"并指向你创建的自定义布局.
也可以尝试使用可能提供更强大功能的ActionProvider.尽管如此,我还没有尝试过这种方法.
Sir*_*lot 10
没有一个答案似乎是完整的,所以经过一些更多的研究后,我想出了:
drawer_switch.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.SwitchCompat
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_switch"
android:layout_width="fill_parent"
android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)
drawer_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="@string/header">
<menu>
<item
android:id="@+id/switch_item"
android:icon="@drawable/ic_switch_item"
app:actionLayout="@layout/drawer_switch"
android:title="@string/switch_item" />
</menu>
</item>
</menu>
Run Code Online (Sandbox Code Playgroud)
DrawerActivity.java:
SwitchCompat drawerSwitch = (SwitchCompat) navigationView.getMenu().findItem(R.id.switch_item).getActionView();
drawerSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// do stuff
} else {
// do other stuff
}
}
});
Run Code Online (Sandbox Code Playgroud)
DrawerActivity.java:
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.switch_item) {
return false;
}
closeDrawer();
return true;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10323 次 |
| 最近记录: |