在导航栏中添加项目

时间:2017-10-04 04:57:19

标签: android navigation-drawer android-navigation-drawer

请在标记副本之前阅读我的问题。

我为导航抽屉的第一个按钮制作了自定义布局,但如何将其包含在导航抽屉项目列表中,所有其他按钮将是默认按钮,只有我想要更改的第一个按钮布局。

1 个答案:

答案 0 :(得分:0)

没问题,只需自定义抽屉。所以从homeActivity或main开始。

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    android:background="@color/colorPrimaryDark">

    <include layout="@layout/fragment_toolbar"/>

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawerlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:openDrawer="start">

        <!-- FrameLayout is used to insert fragments to display -->
        <FrameLayout
            android:id="@+id/fragPlaceholder"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        <!--Left Side Drawer Sliding Menu-->
        <fragment
            android:id="@+id/navDrawerFragment"
            android:name="com.appstudio35.fragments.NavDrawerFragment"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:orientation="vertical"
            tools:layout="@layout/fragment_nav_drawer" />

    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

然后是您的自定义导航视图

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.NavigationView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/nav_drawer_list_background"
    android:fitsSystemWindows="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/navDrawerItemRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/llVersion"
            android:scrollbars="none" />

        <LinearLayout
            android:id="@id/llVersion"
            android:layout_width="match_parent"
            android:gravity="center"
            android:layout_alignParentBottom="true"
            android:layout_height="@dimen/nav_drawer_version_height">

            <ImageView
                android:id="@+id/imgBottomLogo"
                android:layout_width="@dimen/nav_drawer_bottom_logo_width"
                android:layout_height="@dimen/nav_drawer_bottom_logo_height"
                android:src="@drawable/img_ap_menu_logo" />

            <TextView
                android:id="@+id/txtVersion"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/white"
                android:layout_gravity="center"
                android:layout_marginBottom="@dimen/nav_drawer_version_margin_bottom"
                android:textSize="@dimen/nav_drawer_version_text_size"
                android:layout_marginLeft="@dimen/nav_drawer_version_margin_left"
                android:text="@string/nav_drawer_version_text"/>

        </LinearLayout>



    </RelativeLayout>

</android.support.design.widget.NavigationView>

然后,当然只需为动态内容和适配器创建行项目,以填充抽屉中的recyclerview。

行项目示例:

       <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            style="@style/SelectableItemBackground"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:paddingBottom="@dimen/nav_drawer_item_padding_bottom"
                android:paddingTop="@dimen/nav_drawer_item_padding_top"
                android:paddingStart="@dimen/nav_drawer_padding_start"
                android:paddingEnd="@dimen/nav_drawer_padding_end"
                android:gravity="center"
                tools:background="@color/nav_drawer_list_background">

                <RelativeLayout
                    android:layout_width="@dimen/nav_drawer_item_icon_width"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:id="@+id/imgMenuIconDefault"
              android:layout_width="@dimen/nav_drawer_item_menu_icon_width"
            android:layout_height="@dimen/nav_drawer_item_menu_icon_height"
                        tools:src="@drawable/ic_ap_capture_media"
                        tools:visibility="invisible"/>

                    <ImageView
                        android:id="@+id/imgMenuIconSelected"                android:layout_width="@dimen/nav_drawer_item_menu_icon_width"           android:layout_height="@dimen/nav_drawer_item_menu_icon_height"/>

                </RelativeLayout>

                <TextView
                    android:id="@+id/txtMenuName"
                    style="@style/AutoPointTheme.TextView.Medium.Medium"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAllCaps="false"
                    android:text="@string/example_nav_drawer_item"/>

            </LinearLayout>

            <View
                android:id="@+id/menuDivider"
                android:layout_width="match_parent"
              android:layout_height="@dimen/nav_drawer_menu_divider_height"
                android:background="@color/dividerColor" />

        </LinearLayout>
Hope that helps.