我需要将切换到导航抽屉中的项目内。我正在使用新的设计支持库,但我无法找到它是否属于posibble。使用时切换进入安卓设计支持库的导航抽屉项目
android:checkable
项目只是完整的选择,这不是我所希望的。
这是我真正想要的截图。这有可能实现吗?
我需要将切换到导航抽屉中的项目内。我正在使用新的设计支持库,但我无法找到它是否属于posibble。使用时切换进入安卓设计支持库的导航抽屉项目
android:checkable
项目只是完整的选择,这不是我所希望的。
这是我真正想要的截图。这有可能实现吗?
您的抽屉式导航栏菜单项:
<item
android:id="@+id/nav_item1"
android:icon="@drawable/ic_item1"
android:title="item1"
app:actionLayout="@layout/layout_switch"
/>
和该项目的布局:
<?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>
非常感谢。有用。我很遗憾,一年前我不知道。 –
如何为交换机添加监听器,是否可以将代码添加到您的响应中 – mehmet
如何向交换机添加监听器,因为您显然无法以正常方式进行操作。 – mogren3000
您应该可以。
你的抽屉式导航栏视图可以是LinearLayout
了,里面的是,你可以包括你的NavigationView
和另一ViewGroup
添加开关。
你的意思是把
根本是你可以做到这一点很容易,我会后我的代码,这样你就可以实现它
<item android:id="@+id/nav_item_1"
android:title="Android"
android:icon="@drawable/ic_android_black_24dp"
android:checked="true"/>
和主要活动布局应该是:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<TextView
android:id="@+id/content_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:text="Your Content Goes Here"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/colorAccent" />
<!-- your content layout -->
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/menu_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/menu_drawer" />
解决方案中没有开关。 –
我发现这样做的一种方法是使用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);
可能需要一个点击监听,以及改变开关的状态:我还没有尝试
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,它可能会提供更强大一点的功能。但我还没有尝试过这种方法。
我做了这样的事情。
navigationView.getMenu().findItem(R.id.vibrate)
.setActionView(new Switch(this));
Switch vibrateSwitch =(Switch)
navigationView.getMenu().findItem(R.id.vibrate).getActionView();
vibrateSwitch.setChecked(true);
vibrateSwitch.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked){
SharedPreferences sharedPreferences =
getSharedPreferences(getString(R.string.MyPREFERENCES), MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(getString(R.string.VIBRATE), isChecked);
editor.commit();
}
});
你好@mlody,我想问。如何在导航抽屉中添加该开关。我在这里问了一个问题= http://stackoverflow.com/questions/33951901/android-design-navigation-drawer-how-to-add-a-switch-in-nav-xml - 谢谢 – Joolah
@Joolah不幸的是我没有添加这个开关,因为我没有找到方法来做到这一点,没有外部NavigationDrawer。 –