78

Trong ứng dụng Android Honeycomb của tôi, tôi sử dụng Tabs làm kiểu điều hướng. Tôi muốn thêm một mục bên cạnh nút tràn, nhưng tôi muốn mục đó là danh sách thả xuống và người dùng sẽ có thể chọn một tùy chọn ở đó, nhưng không liên quan đến điều hướng. Cách dễ nhất kể từ khi tôi đang sử dụng mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);Cách thêm mục thả xuống trên thanh tác vụ

Có thể thực hiện điều đó mà không cần sử dụng chế độ xem tùy chỉnh không?

+0

Kiểm tra 4 liên kết: Các liên kết này giúp cho bạn http: //fizzylogic.azurewebsites .net/2012/03/05/mono-cho-android-by-example-the-action-bar/https://github.com/StylingAndroid/StylingActionBar https://github.com/StylingAndroid/BasicActionBar – Kumar

Trả lời

206

tùy chọn đầu tiên:

menu/options.xml:

<item 
    android:icon="@drawable/ic_menu_sort" 
    android:showAsAction="ifRoom"> 
    <menu> 
     <item 
      android:id="@+id/menuSortNewest" 
      android:title="Sort by newest" /> 
     <item 
      android:id="@+id/menuSortRating" 
      android:title="Sort by rating" /> 
    </menu> 
</item> 

Thứ hai tùy chọn:

menu/options.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:id="@+id/menuSort" 
     android:showAsAction="ifRoom" 
     android:actionLayout="@layout/action_sort" /> 
</menu> 

bố trí/action_sort.xml:

<Spinner xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/ic_menu_refresh" 
    android:entries="@array/order" /> 

Documents cho nguồn menu - http://developer.android.com/guide/topics/resources/menu-resource.html

+1

Những menu con đó là ag mẹo reat! Đã dành cho tôi nhiều giờ nghiên cứu và làm việc, cảm ơn! – damaxxed

+0

Câu trả lời hay! Btw., Bạn cũng có thể bao gồm các biểu tượng trong menu con. –

+5

Tôi làm cách nào để phản hồi với một lần nhấp của trình quay tròn? – gregm

28

Tuyệt đối là tốt nhất và đơn giản nhất và câu trả lời tôi đã tìm thấy cho đến nay là here.

Về cơ bản, không cần bố cục tùy chỉnh trong trường hợp này. Chỉ cần đặt actonViewClass:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:yourapp="http://schemas.android.com/apk/res-auto" > 

    <item android:id="@+id/spinner" 
    yourapp:showAsAction="ifRoom" 
    yourapp:actionViewClass="android.widget.Spinner" /> <== this is all that's required 
</menu> 

Và sau đó xử lý nó trong onCreateOptionsMenu, như thường lệ:

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_layout, menu); 
    MenuItem item = menu.findItem(R.id.spinner); 
    Spinner spinner = (Spinner) MenuItemCompat.getActionView(item); // get the spinner 
    spinner.setAdapter(adapter); 
    spinner.setOnItemSelectedListener(onItemSelectedListener); 

này đến nay là giải pháp đơn giản nhất và sạch nhất. Tín dụng cho François Poyer, tác giả gốc.

+0

Đã kiểm tra và phê duyệt. –

0

Nó sẽ làm việc như thả xuống chỉ

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 
    <!--<item--> 
    <!--android:id="@+id/save_contact"--> 
    <!--android:icon="@drawable/edit_new"--> 
    <!--android:title="Save Contact"--> 
    <!--app:showAsAction="never" />--> 

    <item 
     android:id="@+id/send_money" 
     android:icon="@drawable/edit_new" 
     android:title="Send Money" 
     app:showAsAction="never" /> 

    <item 
     android:id="@+id/request_money" 
     android:icon="@drawable/edit_new" 
     android:title="Request money" 
     app:showAsAction="never" /> 

    <item 
     android:id="@+id/recharge" 
     android:icon="@drawable/edit_new" 
     android:title="Recharge" 
     app:showAsAction="never" /> 
</menu> 

bên mảnh

setHasOptionsMenu (true)

@Override 
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
    inflater.inflate(R.menu.chat_details_menu, menu); 


    super.onCreateOptionsMenu(menu, inflater); 
} 
Các vấn đề liên quan