Android popupmenu位置 [英] Android popupmenu position

查看:98
本文介绍了Android popupmenu位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个Android应用,其中单击按钮会引发一个 popupmenu . popupmenu 正在生成,但位置不正确.代码如下:

I am trying make an android app where clicking a button raises a popupmenu. The popupmenu is being generated but not at the correct position. The code is as follows:

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>

执行弹出窗口的功能如下:

The function to execute the popup is as follows:

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

当我单击它时,在 textview 的正下方创建 popupmenu .我希望它在屏幕中央生成.

Here the popupmenu is being created just below the textview when I am clicking it. I want it to be generated at the centre of the screen.

如何处理?

推荐答案

如文档所述:

PopupMenu在锚定到视图的模式弹出窗口中显示菜单.如果有空间,弹出窗口将显示在锚视图下方,如果没有空间,则弹出窗口将显示在上方.如果IME可见,则弹出窗口在被触摸之前不会与之重叠.触摸弹出窗口外部将其关闭.

A PopupMenu displays a Menu in a modal popup window anchored to a View. The popup will appear below the anchor view if there is room, or above it if there is not. If the IME is visible the popup will not overlap it until it is touched. Touching outside of the popup will dismiss it.

我可能会猜到,视图v"

As I may guess, that "View v"

public void showGenderPopup(View v)

是您要单击的TextView,单击时绑定到该方法,这意味着PopupMenu将显示在TextView的正下方.

is the TextView you are clicking, which is bound to the method when it is clicked, meaning the PopupMenu will show right below the TextView.

您不会通过对话框实现目标吗?对于自定义AlertDialog,您只需要使用方法

Wouldn't you achieve your goal with a Dialog? For a custom AlertDialog you just need to use method

setView(View v)

AlertDialog.Builder中的

,然后再创建对话框本身.

of the AlertDialog.Builder , before creating the Dialog itself.

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

For your custom View you either follow two ways:

XML:创建您的XML布局文件,然后使用充气器将XML布局应用到View customView对象上.(例如,布局文件称为customDialog.xml)

XML: Create your XML layout file and then using an inflater to apply the XML Layout over a View customView object. (layout file is called customDialog.xml as an example)

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为例.

I'll use LinearLayout as example.

LinearLayout customView = new LinearLayout(context);

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

customView.addView(radioBtn);

要创建对话框,请使用此代码

To create the dialog you then use this code

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

这篇关于Android popupmenu位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆