2016-01-20 88 views
7

我正在开发一个应用程序,其中我正在使用新的设计支持库的导航视图控制器的抽屉菜单。一切工作正常。我使用app:menu属性为导航视图设置了项目。项目完美显示。现在,我为这些商品添加了app:actionLayout,并且它也被添加了,但问题是它正在显示在视图的右侧。导航视图项目的ActionLayout显示在右侧

我的动态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> 

我的菜单文件

<?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> 

我的动作布局文件

<?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问题See the action layout displayed in red in right corner

回答

1

在你的动作布局xml文件,可以解决您的问题添加到根视图(在你的情况LinearLayout)以下属性:

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

但需注意什么文件说,大约actionLayout:

安卓actionLayout: 布局资源。用作操作视图的布局。 https://developer.android.com/guide/topics/resources/menu-resource.html

而一个的actionView:

的动作的观点是,内 应用栏提供丰富的功能的动作。例如,搜索操作视图允许用户在应用栏中键入 他们的搜索文本,而无需更改活动 或片段。 https://developer.android.com/training/appbar/action-views.html

所以也许这是不显示在你的菜单项自定义视图的最佳途径。

看到你的代码,为什么不把图标和文本添加到item元素?

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

但是,如果你真的需要它,显示自定义视图的正确方法是使用ViewHolder模式实现ListView。

相关问题