2014-05-08 30 views
12

Mam PopupMenu, który pojawia się po kliknięciu przycisku akcji na pasku akcji. Chciałbym MenuItem, w moim PopupMenu, z niestandardowy układ jak ten:Menu Android Element niestandardowy Układ

układ/menu_item_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/menuItemLayout" 

    android:orientation="horizontal" > 

    <ImageView 
     android:id="@+id/imageViewMenuItem" 
     android:layout_width="20dip" 
     android:layout_height="20dip" 
     android:src="@drawable/abc_list_focused_holo" /> 

    <TextView 
     android:id="@+id/textViewMenuItem" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextViewMenuItem" /> 

</LinearLayout> 

To xml z PopupMenu:

menu/pop_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     tools:context="apparound.actiobarpopupstylefacebook.Main" > 

    <item 
     android:id="@+id/popupItem" 
     android:showAsAction="ifRoom"/> 
</menu> 

W moim kodu działalności jest następujący:

public void showPopup(int idR){ 
View menuItemView = findViewById(idR); 
PopupMenu popup = new PopupMenu(this, menuItemView); 
MenuInflater inflate = popup.getMenuInflater(); 
inflate.inflate(R.menu.pop_menu, popup.getMenu()); 
MenuItem menuItem= popup.getMenu().findItem(R.id.popupItem); 
menuItem.setActionView(R.layout.menu_item_layout); 
popup.show(); 
} 

Ale gdy pojawiają się menu podręczne, element jest pusty. Myliłem się, aby użyć metody setActionview()? Dzięki.

Odpowiedz

20

przypadku niestandardowych układów nie można korzystać z menu, jedna opcja alternatywna jest PopupWindow

PopupWindow popupwindow_obj = popupDisplay(); 
popupwindow_obj.showAsDropDown(clickbtn, -40, 18); // where u want show on view click event popupwindow.showAsDropDown(view, x, y); 

public PopupWindow popupDisplay() 
{ 

    final PopupWindow popupWindow = new PopupWindow(this); 

    // inflate your layout or dynamically add view 
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View view = inflater.inflate(R.layout.mylayout, null); 

    Button item = (Button) view.findViewById(R.id.button1); 

    popupWindow.setFocusable(true); 
    popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT); 
    popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); 
    popupWindow.setContentView(view); 

    return popupWindow; 
} 

Tworzenie tego pliku XML w folderze res/layout nazwie mój layout.xml

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Window test" /> 
</LinearLayout> 
+0

między Ponad 20 pytań i odpowiedzi, twoja odpowiedź jest najlepsza. musisz dać +500 lub +1000 do przegrania. –

+0

Na wyskakującym okienku pojawia się ciemna ramka –

+0

Naprawiono za pomocą 'popupWindow.setBackgroundDrawable (null);' –

Powiązane problemy