开发者

inflate popup menu items programmatically

开发者 https://www.devze.com 2023-03-02 02:38 出处:网络
I have a button in my activity, pressing upon which, the following method gets called - private ArrayList<ListView> myListViewList;

I have a button in my activity, pressing upon which, the following method gets called -

private ArrayList<ListView> myListViewList;
private ArrayList<MyListAdapter> myAdapterList;

public void onPopupButtonClick(View button) {
    Log.v(TAG, "onPopupButtonClick");

    PopupMenu popup = new PopupMenu(this, button);
    Menu menu = popup.getMenu();

    // how do I display the myListViewList as items of in this popup menu
    // i.e how do I inflate myListViewList into menu object?
}

I know how to do this programmatically for an activity - creating linearl开发者_Go百科ayouts and listViews and finally using addContentView on the activity's context.

But finding it hard to do the same for the popup menu scenario as described above.


Check this code for Pop-up window

 LayoutInflater inflater = (LayoutInflater) PopupMain.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 final View layout = inflater.inflate(R.layout.popup,        
 (ViewGroup)findViewById(R.id.popup_menu_root));
 PopupWindow pw = new PopupWindow(layout, 300,100, true);
 pw.dismiss();
 pw.showAtLocation(layout, Gravity.BOTTOM, 0, 10);

And your New pop-up layout should be as follows :

Popup.xml :

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/popup_menu_root"
   android:background="#FFFFFF"
   android:orientation="vertical"
   android:layout_width="wrap_content"
   android:layout_height="20dip" >
  <TextView android:id="@+id/popup_menu_button1"
     android:text="Copy"
     android:textColor="#000000"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content" />
 </LinearLayout>

This line decide the Height and width of your New View(Pop up window)

  PopupWindow pw = new PopupWindow(layout, 300,100, true);

and pw.showAtLocation(layout, Gravity.BOTTOM, 0, 10);

tells where the Pop-up window should displays..

0

精彩评论

暂无评论...
验证码 换一张
取 消