2012-05-07 20 views
5

Tôi đang cố gắng triển khai thanh tác vụ trong đó một trong các nút khi nhấp chuột sẽ hiển thị menu bật lên. Đây là menu. XML (mục menu trong thanh hành động)Trình đơn bật lên khi nhấp vào nút đang hoạt động Thanh

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

<item 
    android:id="@+id/search" 
    android:icon="@drawable/ic_action_search" 
    android:orderInCategory="0" 
    android:showAsAction="always" 
    android:title="@string/menu_search"/> 
<item 
    android:id="@+id/refresh" 
    android:icon="@drawable/ic_action_refresh" 
    android:orderInCategory="1" 
    android:showAsAction="always" 
    android:title="@string/menu_refresh"/> 


<Item 
    android:id="@+id/popup" 
    android:icon="@drawable/ic_action_search" 
    android:onClick="showPopup" 
    android:orderInCategory="1" 
    android:showAsAction="always" 
    android:title="@string/menu_search" /> 

Tôi muốn hiển thị một popup menu trên nhấp chuột của mặt hàng đó có id "@ + id/cửa sổ bật lên".

đây là XML cho popup menu

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

<item 
    android:id="@+id/item1" 
    android:icon="@drawable/ic_action_search" 
    android:orderInCategory="0" 
    android:showAsAction="always" 
    android:title="@string/menu_search"/> 
<item 
    android:id="@+id/item2" 
    android:icon="@drawable/ic_action_search" 
    android:orderInCategory="1" 
    android:showAsAction="always" 
    android:title="@string/menu_search"/> 

ở đây là phương pháp onClick cho nút

public void showPopup(View v) { 
    PopupMenu popup = new PopupMenu(this, v); 
    MenuInflater inflater = popup.getMenuInflater(); 
    inflater.inflate(R.menu.overflow, popup.getMenu()); 
    popup.show(); 
} 

Và vấn đề được rằng không có sổ popup xuất hiện trên click nút đó. Cần giúp đỡ mọi người.

+0

Bạn có chắc chắn rằng showPopup đang được gọi? Có lẽ onClick không đăng ký đúng cách? – Gophermofur

+0

Tôi không chắc liệu onClick có đăng ký đúng hay không. – darsh

+1

Đặt một log.d hoặc breakpoint trong phương thức showPopup và xem chúng có in bất cứ thứ gì ra/bị đánh hay không. Nếu nó hoạt động, thì bạn có thể tập trung vào mã trong showPopup. – Gophermofur

Trả lời

1

Tôi đã tìm thấy giải pháp cho điều này. Thay vì sử dụng trình đơn XML để làm nổi bật menu bật lên, tôi đã tạo một tệp bố cục XML.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/container" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:background="#8b8989" 
android:orientation="vertical" 
android:padding="10dip" > 

<TextView 
    android:id="@+id/menuItem1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dip" 
    android:text="@string/menu1" /> 

<TextView 
    android:id="@+id/menuItem2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dip" 
    android:text="@string/menu2" /> 
<TextView 
    android:id="@+id/menuItem3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dip" 
    android:text="@string/menu3" /> 
</LinearLayout> 

và tôi đã thay đổi phương pháp onClick

public void showPopup(View v) { 
    LayoutInflater inflater = (LayoutInflater) MainActivity.this 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     PopupWindow pw = new PopupWindow(inflater.inflate(
       R.layout.overflow_layout, null, false), 300, 400, true); 
     pw.showAtLocation(findViewById(R.id.container), Gravity.CENTER, 0, 
       0); 
} 

này đã giải quyết được vấn đề

+3

Tôi không tìm thấy một giải pháp tốt. –

+0

thổi phồng bố cục mỗi lần tốn kém. có một cách sạch hơn, hiệu quả và mạnh mẽ hơn để làm điều này. Nhìn vào câu trả lời của tôi – Nlinscott

0
android:onClick="popup" 

có thể bạn nên đổi thành android:onClick="showPopup"?

+0

oh xin lỗi đó là một sai lầm trong câu hỏi. Thực sự của nó một và giống nhau, tôi đang chỉnh sửa câu hỏi – darsh

3

Khi menu bật lên là MENU, bạn phải xử lý việc này bằng cách triển khai "onOptionsItemSelected". Bạn sẽ có thể nói phải làm gì cho mỗi tùy chọn menu. Nó sẽ thay thế tùy chọn "onClick" mà bạn đã xác định và sẽ được gọi tự động.

2

Cố gắng thay đổi 'this' thành getActivity().

public void showPopup(View v) { 
    PopupMenu popup = new PopupMenu(getActivity(), v); 
    MenuInflater inflater = popup.getMenuInflater(); 
    inflater.inflate(R.menu.overflow, popup.getMenu()); 
    popup.show(); 
} 

Hy vọng nó sẽ giúp .. !!

+0

Tôi đã thử điều này, rất tiếc nó không hoạt động – darsh

+0

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ PopupMenu1.html. Hãy thử điều này nhưng tôi đoán u r làm điều tương tự .. – Rookie

14

Hi mọi người, đó là giải pháp của riêng tôi: tôi đã tạo ra phương pháp showPopup, và sau đó tôi gọi nó là trong onOptionsItemSelected như thế này:

public void showPopup(){ 
    View menuItemView = findViewById(R.id.menu_save); 
    PopupMenu popup = new PopupMenu(getActivity(), menuItemView); 
    MenuInflater inflate = popup.getMenuInflater(); 
    inflate.inflate(R.menu.popup, popup.getMenu()); 
    popup.show(); 

} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    switch (item.getItemId()) { 
    case R.id.menu_save:  
     showPopup(); 
     return true; 
    default: 
     return super.onOptionsItemSelected(item); 
} 

} 

popup.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" > 
<item 
    android:id="@+id/decon" 
    android:showAsAction="ifRoom" 
    android:title="@string/decon"/> 

<item 
    android:id="@+id/mRes" 
    android:showAsAction="ifRoom" 
    android:title="@string/mesRes"/> 

</menu> 

chính.xml => nó được gọi là onCreateOptionsMenu

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

<item 
    android:id="@+id/menu_save" 
    android:enabled="true" 
    android:icon="@drawable/action_save" 
    android:showAsAction="ifRoom|withText" 
    android:title="@string/action_save" 
    android:visible="true"/> 

</menu> 

Cuối cùng tôi

implements PopupMenu.OnMenuItemClickListener to @Override onMenuItemClick method. 
+0

Nó làm việc cho tôi :) –

+0

Cảm ơn rất nhiều, thực sự đã giúp. –

14

Tôi thấy điều này ở đây: http://developer.android.com/guide/topics/ui/menus.html

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

    <item android:id="@+id/selectImg" 
     android:icon="@android:drawable/ic_dialog_dialer" 
     android:showAsAction="always"> 

     <menu> 
      <item android:id="@+id/top" 
       android:title="@string/topimg"/> 
      <item android:id="@+id/bottom" 
       android:title="@string/botimg" /> 
     </menu> 

    </item> 
</menu> 

Bạn có thể đặt một menu trong menu để trình bày sub-menu khi mục được nhấp. Sau đó, trong Java, bạn có thể sử dụng các phương thức giống như bình thường.

@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu items for use in the action bar 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.main, menu); 
     return super.onCreateOptionsMenu(menu); 
    } 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle presses on the action bar items 
     // View v = findViewById(R.id.f); 
     switch (item.getItemId()) { 
      case R.id.top: 
       //action 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

Id của 'top' trong xml vẫn được nhận diện mặc dù đó là menu phụ. Điều này làm việc cho tôi và nó trông giống như menu popup.

+0

Câu trả lời này phải được chấp nhận – mdikici