导航视图多行文字 [英] Navigation View Multiline Text
问题描述
我有一个带有DrawerLayout导航视图的应用程序.我通过编程方式在菜单中添加了项目,因为我是通过网络接收信息的.但是有时,商品名称可能会很长,甚至会被剪掉,甚至会带有椭圆形图标"...".
I have an app with a Navigation View for my DrawerLayout. I add programmatically items in the menu because I receive the information over the network. But sometimes, an item name can be very long and is just cut off with even having the ellipsize icon "..."
有人对我的菜单项多行有想法吗?
Does someone have an idea on how to have multiline for my menu items?
谢谢
推荐答案
从Android支持设计库覆盖 design_navigation_menu_item.xml 并修改所需的内容(设置android:ellipsize="end"
和android:maxLines="2"
)
Override design_navigation_menu_item.xml from the Android Support Design Library and modify the things you need (set android:ellipsize="end"
and 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>
但是,您应该将文本加长以符合材料设计准则.
However, you should just ellipsize the text to properly follow the Material Design guidelines.
您不必重写任何内容就可以将文本省略掉:
You have not to override anything to just ellipsize the text:
- 向 styles.xml 添加新样式
- 将新样式设置为
NavigationView
- Add a new style to the styles.xml
- Set the new style to the
NavigationView
res/values/styles.xml
<style name="TextAppearance">
<item name="android:ellipsize">end</item>
</style>
res/布局/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" />
这篇关于导航视图多行文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!