【问题标题】:Navigation View Multiline Text导航视图多行文本
【发布时间】:2016-08-12 07:34:35
【问题描述】:

我的 DrawerLayout 有一个带有导航视图的应用程序。我以编程方式在菜单中添加项目,因为我通过网络接收信息。但有时,项目名称可能会很长,甚至会被截断,甚至带有椭圆形图标“...”

有人知道如何为我的菜单项设置多行吗?

谢谢

【问题讨论】:

    标签: android navigation-drawer multiline


    【解决方案1】:

    看看这个answer。默认的 android 幻灯片菜单不是很灵活,很可能您必须创建自定义视图。
    我真的 this AndroidHive 的教程。

    【讨论】:

      【解决方案2】:

      从 Android 支持设计库中重写 design_navigation_menu_item.xml 并修改您需要的东西(设置 android:ellipsize="end"android:maxLines="2"

      您的 res/layout/design_navigation_menu_item.xml 应如下所示:

      <?xml version="1.0" encoding="utf-8"?>
      <merge xmlns:android="http://schemas.android.com/apk/res/android">
          <CheckedTextView
              android:id="@+id/design_menu_item_text"
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="1"
              android:drawablePadding="@dimen/design_navigation_icon_padding"
              android:gravity="center_vertical|start"
              android:textAppearance="@style/TextAppearance.AppCompat.Body2"
              android:ellipsize="end"
              android:maxLines="2" />
          <ViewStub
              android:id="@+id/design_menu_item_action_area_stub"
              android:inflatedId="@+id/design_menu_item_action_area"
              android:layout="@layout/design_menu_item_action_area"
              android:layout_width="wrap_content"
              android:layout_height="match_parent" />
      </merge>
      

      但是,您应该只省略文本以正确遵循 Material Design 指南。

      你不必重写任何东西来省略文本:

      • styles.xml 添加新样式
      • 将新样式设置为NavigationView

      res/values/styles.xml

      <style name="TextAppearance">
          <item name="android:ellipsize">end</item>
      </style>
      

      res/layout/activity_main.xml

      <android.support.design.widget.NavigationView
          android:id="@+id/nav_view"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          android:fitsSystemWindows="true"
          app:headerLayout="@layout/nav_header_main"
          app:menu="@menu/activity_main_drawer"
          app:theme="@style/TextAppearance" />
      

      【讨论】:

      • 很好.. 在浪费了很多时间之后。得到解决方案。谢谢
      • 很好的答案:)
      • 谢谢。使用以下样式,我设法在不覆盖任何内容的情况下显示 2 行:
      • 这个解决方案曾经运行良好。但是现在,当我构建应用程序时,design_navigation_menu_item.xml 文件正在恢复到其原始版本。有人有解决方法吗? (顺便说一句,我已经尝试过离线构建应用程序,但徒劳无功)
      • 选项菜单(工具栏右侧的系统弹出项)如何实现?
      【解决方案3】:

      我遇到了同样的问题,我试了又试,Menu 没有任何进展,我不是说这是不可能的,但我找不到打破MenuItem 行的方法。

      但如果您不坚持使用Menu,我建议您在布局中使用ListViewRecyclerView,如下所示:

      <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_draw_header">
      
          <android.support.v7.widget.RecyclerView
              android:id="@+id/rv"
              android:layout_marginTop="160dp"
              android:layout_width="match_parent"
              android:layout_height="match_parent"/>
      
      </android.support.design.widget.NavigationView>
      

      正如你所看到的,没有什么特别的东西要解释,没有菜单,只是一个RecyclerViewlayout_marginTop 值与标题高度相同,我相信你知道故事的其余部分(如何启动RecyclerView),这种方法为NavigationView 菜单项(实际上是RecyclerView 项)提供了更大的灵活性。 如果我的回答不够清楚,请告诉我。

      结果如下:

      【讨论】:

        【解决方案4】:

        你不应该改变它。

        查看材料设计指南:https://material.io/design/components/navigation-drawer.html#anatomy

        【讨论】:

        • 就个人而言,在某些情况下,我认为违反 Material Design 准则并不一定意味着糟糕的 UI 设计。这是一个这样的案例(对于我的实现)
        • 为了支持@Kathir 所说的,它们被称为“指南”而不是“规则”是有原因的。基本上:“这是你的应用程序,让它成为你自己的。这里有一些有用的指南来帮助你设计。随意使用它们......或者不使用”
        【解决方案5】:

        只需在 NavigationView 标签上添加app:itemMaxLines="2" 属性,如下所示:

        <com.google.android.mayerial.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer"
        app:itemMaxLines="2" />
        

        注意:材质依赖是--

        implementation 'com.google.android.material.material:1.0.0'
        

        【讨论】:

        • 注意:正确的版本是1.1.01.0.0材料库中没有这样的attr或方法
        【解决方案6】:

        在 NavigationDrawer 中添加一个样式,将行数声明为“两”,如下所示:

         <style name="NavigationDrawerStyle">
                <item name="android:lines">2</item>
            </style>
        

        在你看来:

        <android.support.design.widget.NavigationView
                android:id="@+id/nav_view"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                android:background="@drawable/estilo"
                android:fitsSystemWindows="true"
                app:itemTextColor="@color/white"
                app:theme="@style/NavigationDrawerStyle"
                app:menu="@menu/activity_main_drawer" />
        

        【讨论】:

          【解决方案7】:

          向 NavigationDrawer 添加样式,然后在样式中包含 2 行。

          <android.support.design.widget.NavigationView
              android:id="@+id/nav_view"
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:layout_gravity="start"
              android:background="@drawable/estilo"
              android:fitsSystemWindows="true"
              app:itemTextColor="@color/white"
              app:theme="@style/NavigationDrawerStyle"
              app:menu="@menu/activity_main_drawer" />
          
          <style name="NavigationDrawerStyle">
              <item name="android:lines">2</item>
          </style>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-08-31
            • 1970-01-01
            相关资源
            最近更新 更多