是否可以自定义Android PopupMenu?

时间:2017-06-16 10:52:08

标签: android android-layout android-view android-menu

是否可以自定义PopupMenu布局?我只需要圆角,是否可以使用库存定制?

1 个答案:

答案 0 :(得分:0)

对于自定义布局,请勿使用PopupMenu,备用选项为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.my_popupwindow_layout, null);


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

    return popupWindow;
}

my_popupwindow_layout.xml文件夹中创建此res/layout文件,该文件夹用于为popupWindow扩充自定义布局。

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