How to set ellipsize in menu item of NavigationView?

帅比萌擦擦* 提交于 2019-12-11 10:34:24

问题


I want set android:ellipsize="end" in menu items of NavigationView. In my current implementaion when text in menu item is too long, it's just cut at the end. Here is what I've tried so far:

<android.support.design.widget.NavigationView
        app:itemTextAppearance="@style/AppTheme.Widget.NavigationView.TextAppearance"
        android:id="@+id/navigation"
        style="@style/AppTheme.Widget.NavigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/drawer_view"/>

<style name="AppTheme.Widget.NavigationView" parent="Widget.Design.NavigationView">
        <item name="android:textAppearance">@style/AppTheme.Widget.NavigationView.TextAppearance</item>
        <item name="android:background">@color/drawer_background</item>
        <item name="itemTextColor">@color/color_drawer_item</item>
        <item name="itemIconTint">@color/color_drawer_item</item>
</style>

<style name="AppTheme.Widget.NavigationView.TextAppearance">
        <item name="android:ellipsize">end</item>
        <item name="android:maxLines">1</item>
</style>

Edit:

Another possible solution is described here: Android NavigationView : not show full item and not truncate


回答1:


Add a new style to the styles.xml:

res / values / styles.xml

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

Set the new style to the NavigationView:

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:


There are 2 parts to this questions - ellipsize long names and maxLines for very long names. As mentioned in another answer, ellipsize is straightforward. Create a style:

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

Add this style to your NavigationView:

 <android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_drawer_header"
    app:menu="@menu/menu_nav_drawer"
    app:itemTextColor="@color/textColorPrimary"
    app:theme="@style/NavigationMenuItemTextAppearance">
</android.support.design.widget.NavigationView>

To set the number of lines you'd allow the name to show for, it can be done by over-riding the layout file: design_navigation_menu_item.xml. Although as per Material design guidelines, only 1 line is allowed for very long menu option material design navigation drawer

Just copy the support library's into your own design_navigation_menu_item.xml and place in the layout folder. Change maxLines="2"

 <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:maxLines="2"
    android:textAppearance="@style/TextAppearance.AppCompat.Body2"/>


来源:https://stackoverflow.com/questions/36157300/how-to-set-ellipsize-in-menu-item-of-navigationview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!