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.
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
Tôi không chắc liệu onClick có đăng ký đúng hay không. – darsh
Đặ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