2016-01-20 14 views
7

私は新しいデザインサポートライブラリのナビゲーションビューコントローラーを使用してドロワーメニュー用のアプリケーションを開発しています。すべてがうまくいきます。 app:menu属性を使用してナビゲーションビュー用のアイテムを設定しました。アイテムは完全に表示されます。今、私はこれらのアイテムのためにapp:actionLayoutを追加していますが、それも追加されていますが、問題はそれがビューの右側に表示されています。ナビゲーションビュー項目のActionLayoutが右側に表示されます

My活動の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

そのため、おそらくそれはあなたのMenuItemにカスタムビューを表示するための最良の方法ではありませんです。

コードを見て、アイコンとテキストを項目要素に追加するだけではどうですか?

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

しかし、本当に必要な場合、カスタムビューを表示する正しい方法は、ViewHolderパターンでListViewを実装することです。

関連する問題