简体   繁体   English

NavigationView项目的ActionLayout显示在右侧

[英]ActionLayout for navigationview items displays on right side

I'm developing an application where I'm using new design support library's navigation view controller for drawer menu. 我正在开发一个使用新设计支持库的抽屉菜单导航视图控制器的应用程序。 Everything is working fine. 一切正常。 I've set items for navigation view using app:menu attribute. 我已经使用app:menu属性为导航视图设置了项目。 Item's are perfectly displayed. 项目的完美显示。 Now i'm adding app:actionLayout for those item's and it's also getting added but the problem is it's getting displayed at right side in the view. 现在,我正在为这些项目添加app:actionLayout ,并且也正在添加它,但是问题是它正显示在视图的右侧。

My Activity XML 我的活动XML

<?xml version="1.0" encoding="utf-8"?>

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <afour.com.uniapp.customwidgets.CustomTextView
            android:id="@+id/text_toolbarTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="start"
            android:text="@string/app_name"
            android:textColor="@android:color/white"
            android:textSize="20sp"
            app:FontName="@string/type_face_splash" />
    </android.support.v7.widget.Toolbar>


    <FrameLayout
        android:id="@+id/frame_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/toolbar" />

        </RelativeLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@android:color/holo_green_dark"
    android:paddingTop="20dp"
    app:headerLayout="@layout/navigation_header_layout"
    app:paddingStart="0dp"
    app:itemBackground="@android:color/holo_green_light"
    app:itemTextColor="@android:color/white"
    app:menu="@menu/main_menu" />

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

My menu file 我的菜单文件

<?xml version="1.0" encoding="utf-8"?>

<item
    android:id="@+id/nav_about"
    android:title="@string/aboutus"
    app:showAsAction="never" />

<item
    android:id="@+id/nav_logout"
    app:actionLayout="@layout/logout_item_layout"
    app:showAsAction="collapseActionView" />

</menu>

my action layout file 我的动作布局文件

<?xml version="1.0" encoding="utf-8"?>

<ImageButton
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_marginLeft="10dp"
    android:layout_marginStart="10dp"
    android:background="@android:color/transparent"
    android:contentDescription="@string/aboutus"
    android:padding="05dp"
    android:src="@android:drawable/ic_menu_help" />

<afour.com.uniapp.customwidgets.CustomTextView
    android:id="@+id/text_logout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"
    android:gravity="start"
    android:text="@string/logout"
    android:textColor="@android:color/white"
    android:textSize="20sp"
    app:FontName="@string/type_face_regular" />

</LinearLayout>

PFA of the issue 问题的PFA 查看右上角以红色显示的操作布局

In your action layout xml file, you can fix your problem adding to the root view (a LinearLayout in your case) the following properties: 在操作布局xml文件中,您可以解决以下问题,将问题添加到根视图(在本例中为LinearLayout)中:

<LinearLayout
   ...
   android:layout_gravity="start">

But take note what the documentation says about actionLayout: 但是请注意文档中关于actionLayout的内容:

android:actionLayout: Layout resource. android:actionLayout:布局资源。 A layout to use as the action view. 用作动作视图的布局。 https://developer.android.com/guide/topics/resources/menu-resource.html https://developer.android.com/guide/topics/resources/menu-resource.html

And an actionView: 和一个actionView:

An action view is an action that provides rich functionality within the app bar. 动作视图是一种在应用栏中提供丰富功能的动作。 For example, a search action view allows the user to type their search text in the app bar, without having to change activities or fragments. 例如,搜索操作视图允许用户在应用栏中键入其搜索文本,而无需更改活动或片段。 https://developer.android.com/training/appbar/action-views.html https://developer.android.com/training/appbar/action-views.html

So maybe that's isn't the best way to display a custom view in your MenuItem. 因此,也许这不是在MenuItem中显示自定义视图的最佳方法。

Seeing your code, why not just add the icon and text to the item element? 看到您的代码,为什么不只将图标和文本添加到item元素呢?

<item
    android:id="@+id/nav_logout"
    android:icon="@android:drawable/ic_menu_help"
    android:text="@string/logout" />

But if you really need it, the right way to display the custom view is implementing a ListView with ViewHolder pattern. 但是,如果您确实需要它,则显示自定义视图的正确方法是使用ViewHolder模式实现ListView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM