2016-01-20 16 views
7

Tôi đang phát triển một ứng dụng mà tôi đang sử dụng bộ điều khiển chế độ xem điều hướng của thư viện hỗ trợ thiết kế mới cho trình đơn ngăn kéo. Mọi thứ đều hoạt động tốt. Tôi đã đặt các mục cho chế độ xem điều hướng bằng thuộc tính app:menu. Mục được hiển thị hoàn hảo. Bây giờ tôi đang thêm app:actionLayout cho các mục đó và nó cũng được thêm vào nhưng vấn đề là nó được hiển thị ở bên phải trong khung nhìn.Hành độngThanh toán cho các mục điều hướng hiển thị ở bên phải

My Hoạt động 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> 

menu file của tôi

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

file layout hành động của tôi

<?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 của vấn đề See the action layout displayed in red in right corner

Trả lời

1

Trong hành động bố trí tập tin xml của bạn, bạn có thể khắc phục vấn đề của bạn thêm vào xem gốc (một LinearLayout trong trường hợp của bạn) các thuộc tính sau:

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

Nhưng hãy lưu ý những gì các tài liệu nói về actionLayout:

android: actionLayout: nguồn Layout. Bố cục để sử dụng làm chế độ xem hành động. https://developer.android.com/guide/topics/resources/menu-resource.html

Và một ActionView:

An xem hành động là một hành động mà cung cấp chức năng phong phú trong thanh ứng dụng. Ví dụ: chế độ xem tác vụ tìm kiếm cho phép người dùng nhập văn bản tìm kiếm của họ trong thanh ứng dụng mà không phải thay đổi hoạt động hoặc các đoạn. https://developer.android.com/training/appbar/action-views.html

Vì vậy, có lẽ đó không phải là cách tốt nhất để hiển thị chế độ xem tùy chỉnh trong MenuItem của bạn.

Thấy mã của bạn, tại sao không chỉ thêm biểu tượng và văn bản vào phần tử mục?

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

Nhưng nếu bạn thực sự cần nó, đúng cách để hiển thị chế độ xem tùy chỉnh đang triển khai một ListView với mẫu ViewHolder.

Các vấn đề liên quan