Android 水平弹出菜单?
我正试图在单击“共享”按钮时,弹出一个带有三个水平选项的Android 水平弹出菜单?,android,grid,popupmenu,horizontallist,Android,Grid,Popupmenu,Horizontallist,我正试图在单击“共享”按钮时,弹出一个带有三个水平选项的facebook、twitter和google+的弹出菜单 我一直在寻找,但直到现在我什么也没有得到 是否可以创建水平网格或均匀网格PopupMenu? 是否可以在弹出菜单中使用RecyclerView?而不是弹出菜单,您是否可以将对话框片段与自定义布局一起使用?在这种情况下,您可以使用弹出菜单。您可以使用ListView显示每行中的项目。显示弹出窗口的示例代码: public PopupWindow popupWindowsort()
facebook
、twitter
和google+
的弹出菜单
我一直在寻找,但直到现在我什么也没有得到
是否可以创建水平网格或均匀网格PopupMenu
?
是否可以在弹出菜单中使用RecyclerView
?而不是弹出菜单
,您是否可以将对话框片段
与自定义布局一起使用?在这种情况下,您可以使用弹出菜单
。您可以使用ListView
显示每行中的项目。显示弹出窗口的示例代码
:
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;
}
public-PopupWindow-popupWindowsort(){
//初始化弹出窗口类型
popupWindow=新的popupWindow(此);
ArrayList sortList=新建ArrayList();
添加(“Google+”);
sortList.add(“Facebook”);
sortList.add(“推特”);
ArrayAdapter=新的ArrayAdapter(此,
android.R.layout.simple\u下拉列表\u项目\u 1行,排序列表);
//下拉列表是一个列表视图
ListView listViewSort=新建ListView(此);
//设置适配器并传递弹出窗口内容
setAdapter(适配器);
//在选定的项目上设置
setOnItemClickListener(onItemClickListener());
//弹出窗口的一些其他视觉设置
popupWindow.setFocusable(真);
弹出窗口设置宽度(250);
//setBackgroundDrawable(getResources().getDrawable(R.drawable.white));
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_内容);
//将列表视图设置为弹出窗口内容
setContentView(listViewSort);
返回弹出窗口;
}
有关更多详细信息,请访问我的帖子:
希望这有帮助 创建具有内部水平布局的基本布局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>
其实很简单。一个完整的解决方案是:
1-自定义弹出菜单类:
public class PopupMenuCustomLayout {
private PopupMenuCustomOnClickListener onClickListener;
private Context context;
private PopupWindow popupWindow;
private int rLayoutId;
private View popupView;
public PopupMenuCustomLayout(Context context, int rLayoutId, PopupMenuCustomOnClickListener onClickListener) {
this.context = context;
this.onClickListener = onClickListener;
this.rLayoutId = rLayoutId;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = inflater.inflate(rLayoutId, null);
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true;
popupWindow = new PopupWindow(popupView, width, height, focusable);
popupWindow.setElevation(10);
LinearLayout linearLayout = (LinearLayout) popupView;
for (int i = 0; i < linearLayout.getChildCount(); i++) {
View v = linearLayout.getChildAt(i);
v.setOnClickListener( v1 -> { onClickListener.onClick( v1.getId()); popupWindow.dismiss(); });
}
}
public void setAnimationStyle( int animationStyle) {
popupWindow.setAnimationStyle(animationStyle);
}
public void show() {
popupWindow.showAtLocation( popupView, Gravity.CENTER, 0, 0);
}
public void show( View anchorView, int gravity, int offsetX, int offsetY) {
popupWindow.showAsDropDown( anchorView, 0, -2 * (anchorView.getHeight()));
}
public interface PopupMenuCustomOnClickListener {
public void onClick(int menuItemId);
}
}
您仍然需要处理水平列表的水平部分
这对切换方向来说不是有点过分吗?弹出菜单
用于将浮动视图(通常用于选择项目)锚定到当前视图中的特定点,而对话框片段
是一个独立的对话框视图,具有自己的上下文和根。您将以指数方式增加采取垂直布局并使其水平的步骤。
public class PopupMenuCustomLayout {
private PopupMenuCustomOnClickListener onClickListener;
private Context context;
private PopupWindow popupWindow;
private int rLayoutId;
private View popupView;
public PopupMenuCustomLayout(Context context, int rLayoutId, PopupMenuCustomOnClickListener onClickListener) {
this.context = context;
this.onClickListener = onClickListener;
this.rLayoutId = rLayoutId;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = inflater.inflate(rLayoutId, null);
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true;
popupWindow = new PopupWindow(popupView, width, height, focusable);
popupWindow.setElevation(10);
LinearLayout linearLayout = (LinearLayout) popupView;
for (int i = 0; i < linearLayout.getChildCount(); i++) {
View v = linearLayout.getChildAt(i);
v.setOnClickListener( v1 -> { onClickListener.onClick( v1.getId()); popupWindow.dismiss(); });
}
}
public void setAnimationStyle( int animationStyle) {
popupWindow.setAnimationStyle(animationStyle);
}
public void show() {
popupWindow.showAtLocation( popupView, Gravity.CENTER, 0, 0);
}
public void show( View anchorView, int gravity, int offsetX, int offsetY) {
popupWindow.showAsDropDown( anchorView, 0, -2 * (anchorView.getHeight()));
}
public interface PopupMenuCustomOnClickListener {
public void onClick(int menuItemId);
}
}
<?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:background="@color/white"
android:orientation="horizontal">
<TextView
android:id="@+id/popup_menu_custom_item_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="A"
android:textAppearance="?android:textAppearanceMedium" />
<TextView
android:id="@+id/popup_menu_custom_item_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="B"
android:textAppearance="?android:textAppearanceMedium" />
// ...
</LinearLayout>
PopupMenuCustomLayout popupMenu = new PopupMenuCustomLayout(
MainActivity.mainActivity, R.layout.popup_menu_custom_layout,
new PopupMenuCustomLayout.PopupMenuCustomOnClickListener() {
@Override
public void onClick(int itemId) {
// log statement: "Clicked on: " + itemId
switch (itemId) {
case R.id.popup_menu_custom_item_a:
// log statement: "Item A was clicked!"
break;
}
}
});
// Method 1: popupMenu.show();
// Method 2: via an anchor view:
popupMenu.show( anchorView, Gravity.CENTER, 0, 0);