Android popupmenu position

有些话、适合烂在心里 提交于 2019-12-01 03:11:24

As from docs says :

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.

As I may guess, that "View v"

public void showGenderPopup(View v)

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.

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

setView(View v)

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

For your custom View you either follow two ways:

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

DYNAMICALLY :

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();
  PopupMenu popup = new PopupMenu(this, v,Gravity.CENTER);  

use above code. Gravity have many options like center/left/right check the documentation ocne

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!