安卓弹出菜单位置

14

我正在尝试制作一个安卓应用程序,当点击按钮时会弹出一个popupmenu。虽然popupmenu已经生成,但位置不正确。代码如下:

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
    android:id="@+id/genderMale"
    android:title="Male"
/>
<item
    android:id="@+id/genderFemale"
    android:title="Female"
/>
</group>
</menu>

执行弹出窗口的函数如下:

public void showGenderPopup(View v)
{
    PopupMenu popup = new PopupMenu(this, v);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.gender_popup, popup.getMenu());
    popup.show();
}

当我点击文本视图时,这里正在创建一个下拉菜单,我希望它在屏幕中心生成。

如何操作?

2个回答

28
  PopupMenu popup = new PopupMenu(this, v,Gravity.CENTER);  

使用上述代码。Gravity有许多选项,如居中/左对齐/右对齐,请查看文档。


1
不起作用。可能是因为这里的视图是TextView,而不是整个屏幕。这是我的结论,虽然我不知道我有多正确,但这就是我认为它不起作用的原因。 - praxmon
将文本视图的宽度设置为match_parent,然后再试一次。 - RamBabu Pudari
尝试过了,完全没有改变 :/ - praxmon
抱歉,伙计,这是一个300行的代码,我真的不想这样做。请耐心等待好吗? - praxmon

4

根据文档所述:

PopupMenu 会在模态弹出窗口中显示菜单,该窗口固定于某个视图上。如果有足够的空间,弹出窗口将出现在锚定视图下方;否则,弹出窗口将出现在其上方。如果 IME 可见,则在触摸之前不会重叠。在弹出窗口以外触摸将关闭它。

我猜测,这里的“View v”是指某个视图。

public void showGenderPopup(View v)

你点击的是TextView,它绑定了点击时要执行的方法,这意味着PopupMenu会在TextView正下方显示。

你不想用对话框实现目标吗?对于自定义AlertDialog,你只需要使用方法

setView(View v)

在创建Dialog本身之前,需要使用AlertDialog.Builder对其进行设置。

对于自定义视图,您可以采用以下两种方法之一:

XML: 创建您的XML布局文件,然后使用Inflater将XML布局应用于一个View customView对象。(例如,布局文件称为customDialog.xml)

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

View customView = inflater.inflate(R.layout.customDialog, null);    

RadioButton radioButton = (RadioButton) customView.findViewById(R.id.customDialogRadioButton);
radioButton.setOnClickListener(new OnClickListener() { .. });

动态地:

我将以LinearLayout为例。

LinearLayout customView = new LinearLayout(context);

RadioButton radioBtn = new RadioButton(context); 
radioBtn.setOnClickListener(new OnClickListener() { .. });

customView.addView(radioBtn);

为了创建对话框,您可以使用以下代码。
AlertDialog.Builder b = new AlertDialog.Builder(context);
b.setMessage("Example");

// set dialog's parameters from the builder

b.setView(customView);

Dialog d = b.create();
d.show();

2
事实上,我还要在菜单上激活组,以便获得单选按钮。不认为我可以使用AlertDialog来实现这一点,因为它会给我提供按钮的选择,而我不想使用它们。但是,如果它确实能够给我单选按钮,我会使用它的。我会尽快搜索谷歌。 - praxmon
1
请记住,您可以始终设置自定义视图以在对话框内使用。我自己使用了一个带有4个单选按钮的对话框,创建了一个易于使用的SQLite工具,以便在应用程序中直接调试我的数据库。您必须创建自定义视图的实例,在其中添加所需的单选按钮。完成后,只需像文档中描述的那样使用b.setView(customView);即可。 http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setView(android.view.View) - FrancescoC
1
你可以按照XML的方式,创建并使用一个inflater将XML布局应用于自定义视图View customView;对象。`LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View customView = inflater.inflate(R.layout.customDialog, null);b.setView(customView);`或者你可以动态地创建它。我将使用LinearLayout作为示例。LinearLayout linearLayout = new LinearLayout(context); RadioButton radioBtn = new RadioButton(context); linearLayout.addView(radioBtn); - FrancescoC
1
你能否将这个加入到你的回答中,以提高可读性。我想这也会提高你的回答质量。 - praxmon
非常感谢!谢谢!! - praxmon
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接