[英]How can I use PopupWindow in Xamarin android?
我正在尝试使用 PopupWindow 来膨胀布局,但我在互联网上没有找到任何示例。 能不能教教我怎么用。
这是我当前的代码,但没有显示 popupWindow
private void ShowMenu(ImageButton anchor, Dictionary<string, object> data)
{
LayoutInflater inflater = (LayoutInflater)_context.GetSystemService(Context.LayoutInflaterService);
View popupView = inflater.Inflate(Resource.Layout.menu_outlet_buttons, null);
int width = LinearLayout.LayoutParams.WrapContent;
int height = LinearLayout.LayoutParams.WrapContent;
bool focusable = true;
PopupWindow popupWindow = new PopupWindow(popupView.Context);
popupWindow.ShowAtLocation(_view, GravityFlags.Center, 0, 0);
}
顺便说一句,这是一个适配器
public MyAdapter(List<Dictionary<string, object>> data, Context context)
{
_data = data;
_context = context;
}
这是我的参考,但我无法在 Xamarin Android 中重现相同的内容。
您创建 PopupWindow 实例的方法是错误的。 我在我的项目中尝试了以下代码,效果很好:
View view = LayoutInflater.Inflate(Resource.Layout.popup, null);
PopupWindow popupWindow = new PopupWindow(view,LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent,true);
popupWindow.ShowAtLocation(view, GravityFlags.Center, 0, 0);
更新:
我检查了 popupWindow.ShowAtLocation 方法,第一个参数是父视图。
您可以通过以下代码获取 x,y:
int[] location = new int[2];
imagebutton.GetLocationOnScreen(location);
int x = location[0];
int y = location[1];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.