以编程方式膨胀弹出菜单项

时间:2011-05-03 10:30:18

标签: android

我的活动中有一个按钮,按下该按钮,将调用以下方法 -

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?
}

我知道如何以编程方式为活动创建 - 创建linearlayouts和listViews,最后在活动的上下文中使用addContentView。

但是发现如上所述的弹出菜单场景很难做到这一点。

1 个答案:

答案 0 :(得分:0)

检查此代码是否为弹出窗口

 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);

您的新弹出式布局应如下所示:

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>

此行决定新视图的高度和宽度(弹出窗口)

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

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

告诉弹出窗口应显示的位置..

相关问题