问题
I am new in android and in my project I want to add the navigation drawer. I am selecting Navigation Drawer activity and create project in eclipse please help me how to develop navigation drawer below
Here is the image of navigation drawer for item and sub item menu
回答1:
Use NavigationView
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header_main_navigation"
app:itemBackground="@drawable/nav_view_item_background"
app:itemTextColor="@color/nav_item_text_color"
app:menu="@menu/activity_main_navigation_drawer" />
activity_main_navigation_drawer code
<?xml version="1.0" encoding="utf-8"?>
<group android:checkableBehavior="single" android:id="@+id/root_group">
<item
android:id="@+id/item_home"
android:checked="true"
android:title="Home" />
<item android:title="You" android:id="@+id/you_parent_item">
<menu android:id="@+id/you_menu">
<group android:checkableBehavior="single" android:id="@+id/you_group">
<item
android:id="@+id/item_you_summary"
android:title="Summary Detail" />
<item
android:id="@+id/item_calendar"
android:title="Calendar" />
<item
android:id="@+id/item_request_timeout"
android:title="Request TimeOut" />
<item
android:id="@+id/item_cancel_requests"
android:title="Cancel Requests" />
<item
android:id="@+id/item_approval_responses"
android:title="Approval Responses" />
<item
android:id="@+id/item_transaction"
android:title="Transactions" />
<item
android:id="@+id/item_approval_request"
android:title="Approval Requests" />
<item
android:id="@+id/item_delegate"
android:title="Delegate" />
</group>
</menu >
</item>
<item android:title="Your People" android:id="@+id/your_parent_item">
<menu android:id="@+id/your_menu">
<group android:checkableBehavior="single" android:id="@+id/your_group">
<item
android:id="@+id/your_item_sumary"
android:title="View Summary" />
<item
android:id="@+id/item_record_timeout"
android:title="Record TimeOut" />
</group>
</menu>
</item>
<item android:title="Settings" android:id="@+id/setting_parent_item">
<menu android:id="@+id/setting_menu">
<group android:checkableBehavior="single" android:id="@+id/setting_group">
<item
android:id="@+id/item_log_out"
android:title="Log Out" />
</group>
</menu>
</item>
</group>
HeaderLayout if you want sent then
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@color/cws_orange"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:theme="@style/Theme.AppTheme.AppBarOverlay">
<ImageView
android:id="@+id/imgAvatar"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@android:drawable/sym_def_app_icon" />
<TextView
android:id="@+id/txtUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="Android Studio"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="@+id/txtUserEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android.studio@android.com" />
来源:https://stackoverflow.com/questions/34542482/how-to-implement-the-navigation-drawer-in-android-with-sub-item-menu