java - 更改 NavigationView 中项目的样式

标签 java android xml androiddesignsupport navigationview

我有一个 DrawerLayout,里面有一个 NavigationView(支持设计库)。我已经能够设置标题并将项目添加到 View 中,但我希望某些项目具有更小的文本、没有图标等...我无法弄清楚我将在哪里指定新属性。在我的 drawer.xml 中,其他项目所在的位置,我不知道哪些属性可以更改它。

感谢任何建议!

Current Look (Image)

我有两个项目具有预期的外观和最后一个项目,注销,我想做得更小,并有一个分隔符的开销。

Desired Look (Image)

layout/activity_main.xml

<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/fragment_navdrawer_header"
        app:menu="@menu/drawer"/>

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

menu/drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/navigation_item_1"
            android:icon="@drawable/ic_action_social_school"
            android:title="Courses"/>
        <item
            android:id="@+id/navigation_item_2"
            android:icon="@drawable/ic_action_action_grade"
            android:title="Grades"/>
        <item
            android:id="@+id/navigation_logout"
            android:title="Logout"
           />
    </group>
</menu>

最佳答案

您的 activity_main.xml 应该是这样的。工具栏被注释掉了,但如果你需要它,请取消注释并添加一个工具栏布局。在页面底部添加注销按钮和分隔符 View ,并将它们设置为与 ListView 中的项目一样的样式。

    <android.support.v4.widget.DrawerLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/DrawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:elevation="7dp">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


    /*    <include
            android:id="@+id/tool_bar"
            layout="@layout/tool_bar">
        </include> */

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World" />

    </LinearLayout>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/RecyclerView"
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"

        android:background="#ffffff"
        android:scrollbars="vertical">

    </android.support.v7.widget.RecyclerView>

//devider 

  <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:alpha="0.2"
                android:background="@android:color/white" />
//logout button 
  <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="12dp"
        android:paddingTop="4dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/txtLogout" />

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

代替 drawer.xml 添加新的 item_row 布局。这对于 recyclerView 中的每一行

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:paddingTop="8dp"
    android:paddingBottom="8dp"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:orientation="horizontal">


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rowIcon"
        android:paddingLeft="16dp"
        android:src="@drawable/ic_launcher"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="12dp"
        android:paddingTop="4dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/rowText" />

</LinearLayout>

这只是抽屉外观的草图,但您可以重新设计标题以显示图像和所有内容。下面是几个很好的链接,详细解释了这一点。

How To Make Material Design Navigation Drawer With Header View

Android Getting Started with Material Design

希望对你有帮助

关于java - 更改 NavigationView 中项目的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31095503/

相关文章:

android - 在 Android SQLite 中检索两个日期之间的数据

java.lang.NoClassDefFoundError : Failed resolution of: Ljava/lang/invoke/MethodHandles$Lookup 错误

Android AVD 模拟器缺少字母/元素

xml - 从 OpenERP 中的按钮启动向导

java - Android - 关闭第一个 Activity

java - 我正在尝试创建一个简单的 hibernate 应用程序。它给出以下错误。如何解决该错误

java - 将命令行参数传递给可运行的 JAR

java - 以编程方式操作 MP3 : Muting certain parts?

java - 关于android蓝牙要求的问题

xml - 通过 JAXB 验证 XML