[我试图单击Popupmenu
时显示共享按钮,并显示三个水平选项facebook
,twitter
和google+
。
我一直搜索了一段时间,但直到现在我什么都没得到。
是否可以创建水平或均匀网格PopupMenu
?是否可以在RecyclerView
中使用PopupMenu
?
您可以使用自定义布局的PopupMenu
而不是DialogFragment
?
在这种情况下,您可以使用PopupWindow
。您可以使用ListView
在每行中显示项目。显示PopupWindow
的示例代码:
public PopupWindow popupWindowsort() {
// initialize a pop up window type
popupWindow = new PopupWindow(this);
ArrayList<String> sortList = new ArrayList<String>();
sortList.add("Google+");
sortList.add("Facebook");
sortList.add("Twitter");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, sortList);
// the drop down list is a list view
ListView listViewSort = new ListView(this);
// set our adapter and pass our pop up window contents
listViewSort.setAdapter(adapter);
// set on item selected
listViewSort.setOnItemClickListener(onItemClickListener());
// some other visual settings for popup window
popupWindow.setFocusable(true);
popupWindow.setWidth(250);
//popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.white));
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
// set the list view as pop up window content
popupWindow.setContentView(listViewSort);
return popupWindow;
}
访问我的帖子以获取更多详细信息:http://www.devexchanges.info/2015/02/android-popupwindow-show-as-dropdown.html。希望有帮助!
创建具有内部水平布局的基本布局XML。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/popup_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
将布局分配给PopupMenu,然后使用内部布局。
// PopupMenu accepts ViewGroup, so cast the inflated layout view
// Replace ROOT_VIEW_HERE with the parent view of the PopupMenu
LinearLayout popupWindow = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.popup_window, ROOT_VIEW_HERE);
PopupMenu popupMenu = new PopupMenu(this, popupWindow);
LinearLayout innerView = popupWindow.findViewById(R.id.popup_horizontal);
真的就是这么简单。