2015-05-06 16 views
8

Tôi muốn hiển thị menu mục bổ sung trên thanh công cụ (AppCompat-v7: 22.1.1), bên dưới là menu_main.xml của tôi.Tình trạng lạ này xảy ra như thế nào khi hiển thị biểu tượng mục trình đơn trong menu mục bổ sung trên thanh công cụ?

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
tools:context=".MainActivity"> 
<item 
    android:id="@+id/action_search" 
    android:title="@string/action_search" 
    android:icon="@mipmap/ic_menu_search" 
    android:orderInCategory="100" 
    android:actionViewClass="android.widget.SearchView" 
    app:showAsAction="ifRoom"/> 

<item 
    android:id="@+id/menu_group_chat" 
    android:title="@string/menu_group_chat" 
    android:icon="@mipmap/ic_menu_groupchat" /> 

<item 
    android:id="@+id/menu_add_friend" 
    android:title="@string/menu_add_friend" 
    android:icon="@mipmap/ic_menu_add_friend" /> 

Sau khi chạy ứng dụng của tôi, biểu tượng của mục menu không hiển thị, sau đó tôi đã cố gắng solution này, thêm một phương pháp ghi đè onMenuOpened() trong Activty tôi (kéo dài từ AppCompatActivity),

@Override 
public boolean onMenuOpened(int featureId, Menu menu) { 
    if(menu!=null){ 
     if(menu.getClass().getSimpleName().equals("MenuBuilder")){ 
      try { 
       Method m = menu.getClass().getDeclaredMethod(
         "setOptionalIconsVisible", Boolean.TYPE); 
       m.setAccessible(true); 
       m.invoke(menu, true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
    return super.onMenuOpened(featureId, menu); 
} 

Nhưng sau khi chạy bản trình diễn này, tôi thấy biểu tượng vẫn không được hiển thị.

1. Menu with overflow button click

Từ reported issue này, tôi biết rằng AppCompatActivity.onMenuOpened không được gọi nữa trong 22.x, nhưng nó lẻ rằng khi tôi bấm phím menu phần cứng trong Genymotion, menu xuất hiện ở phía dưới và với biểu tượng,

2. Menu with menu key click

sau khi đóng trình đơn, tôi bấm vào nút tràn trong thanh công cụ một lần nữa, các biểu tượng trong menu xuất hiện,

3. Menu with overflow button click

mức độ lạ! Tại sao điều này xảy ra?

Trả lời

21

Đối với AppCompactActivity bạn có thể đặt việc kiểm tra này trên onPrepareOptionsPanel() để thay thế.

@Override 
protected boolean onPrepareOptionsPanel(View view, Menu menu) { 
     if (menu != null) { 
      if (menu.getClass().getSimpleName().equals("MenuBuilder")) { 
       try { 
        Method m = menu.getClass().getDeclaredMethod(
          "setOptionalIconsVisible", Boolean.TYPE); 
        m.setAccessible(true); 
        m.invoke(menu, true); 
       } catch (Exception e) { 
        Log.e(getClass().getSimpleName(), "onMenuOpened...unable to set icons for overflow menu", e); 
       } 
      } 
     } 
    return super.onPrepareOptionsPanel(view, menu); 
} 
+0

Nó có thể là một vấn đề của hương vị nhưng tôi nghĩ rằng 'nếu (menu.getClass(). Equals (MenuBuilder.class))' là hơn tao nhã hơn 'nếu (menu.getClass(). getSimpleName(). bằng ("MenuBuilder")) '. Các chuỗi ít mã hóa càng tốt IMHO ;-) – Matthias

+0

Bỏ qua nhận xét của tôi. Làm theo cách của bạn làm cho nó hoạt động cho cả MenuBuilder và AppCompat của Android. Công việc tốt! – Matthias

+2

Điều này phải được chấp nhận làm câu trả lời. – Chitrang

0

Đây là một sửa đổi của câu trả lời tuyệt vời được cung cấp ở trên bởi Alécio Carvalho. Sửa đổi này là dành cho trường hợp nếu cần hiển thị chính xác các biểu tượng không nằm trong thanh tác vụ của ứng dụng chính, nhưng trong thanh công cụ tùy chỉnh bên trong từng đoạn riêng biệt (Tôi muốn có một thanh công cụ riêng biệt với tiêu đề riêng và menu hành động tùy chỉnh riêng cho từng đoạn, không chỉ đơn giản là thêm các mục mới vào thanh hành động của AppCompatActivity tổng thể).

Đối với trường hợp đề cập đến lớp Fragment là như sau:

public class MyFragment extends android.support.v4.app.Fragment { 
... 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     //at first get the very toolbar 
     fragmentToolbar = (Toolbar) view.findViewById(R.id.fragment_toolbar); 
     fragmentToolbar.setTitle(R.string.title_string); 
     fragmentToolbar.showOverflowMenu(); 

     //now ready to get the menu's method, which is responsible for icons, and change its properties 
     Menu menu=fragmentToolbar.getMenu(); 
     Method menuMethod = null; 
     try { 
      menuMethod = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE); 
      menuMethod.setAccessible(true); 
      menuMethod.invoke(menu, true); 
     } catch (NoSuchMethodException e) { 
      e.printStackTrace(); 
     } catch (InvocationTargetException e) { 
      e.printStackTrace(); 
     } catch (IllegalAccessException e) { 
      e.printStackTrace(); 
     } 

     //now all the other stuff necessary for the toolbar operation 
     fragmentToolbar.inflateMenu(R.menu.my_fragment_menu); 
     fragmentToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { 
      @Override 
      public boolean onMenuItemClick(MenuItem arg0) { 
       if(arg0.getItemId() == R.id.menu_item_1){ 
        ... 
       } 
       return false; 
      } 
     }); 

     //and now the main stuff of onCreateView 
     View view = inflater.inflate(R.layout.my_fragment_layout, container, false); 
     return view; 
    } 
} 

Sau đó my_fragment_layout.xml bao gồm thực đơn như sau

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:id="@+id/fragment_toolbar" 
     android:layout_height="wrap_content" 
     android:background="@color/colorPrimary" 
     android:elevation="4dp"> 
    </android.support.v7.widget.Toolbar> 

//...other items 

</LinearLayout> 

Một tập tin trình đơn điển hình đã được thực hiện như res/menu/my_fragment_menu.xml. Đoạn đã được bổ sung trong cách bố trí của MainActivity đơn giản là

<fragment android:id="@+id/my_fragment_id" 
android:name="com.appspot.trendy.trendychart.MyFragment" 
android:layout_width="match_parent" 
android:layout_height="match_parent"/> 
Các vấn đề liên quan