Android 弹出窗口,如Play Store中所示
我想创建一个与播放商店相同的弹出菜单。弹出菜单应在网格视图项的右下角打开。但如果项目位于屏幕底部,则弹出窗口应在单击点上方打开 我尝试过弹出菜单,但它会在项目的下方或顶部打开Android 弹出窗口,如Play Store中所示,android,android-popupwindow,Android,Android Popupwindow,我想创建一个与播放商店相同的弹出菜单。弹出菜单应在网格视图项的右下角打开。但如果项目位于屏幕底部,则弹出窗口应在单击点上方打开 我尝试过弹出菜单,但它会在项目的下方或顶部打开 category_GV.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int posit
category_GV.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
System.out.println("position is : " + position);
final MenuItems menuItems = (MenuItems)parent.getItemAtPosition(position);
final String default_topping_link = menuItems.getLink();
try{
topping = DefaultToppingParser.parse(new FileInputStream(default_topping_link));
for(int i=0;i<topping.size();i++){
System.out.println("topping id : " + topping.get(i));
}
}catch(Exception e){
e.printStackTrace();
}
if(position == (burger_item_AL.size()-1)){
Intent intent = new Intent(MainActivity.this,CustomiseItem.class);
intent.putExtra("default_toppings_id", base_id);
System.out.println("intent");
startActivity(intent);
} else {
PopupMenu popupMenu = new PopupMenu(MainActivity.this, view);
popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());
popupMenu.show();
popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
....some coding
return true;
}
});
}
}
});
显示弹出式方法:
public static void showPopup(final Context context, Point p) {
int popupWidth = 150;
int popupHeight = 150;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) ((Activity) context).findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup, viewGroup);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
// Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
int OFFSET_X = 30;
int OFFSET_Y = 30;
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
System.out.println("showing popup");
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x - OFFSET_X, p.y + OFFSET_Y);
}
你应该使用弹出窗口来模仿你感兴趣的行为 通过这个链接,你可以找到一个有趣的教程,教你如何做到这一点。 希望有帮助。main.xml
<?xml version="1.0" encoding="utf-8"?>
此代码非常适合我,我的要求与您描述中提到的相同,希望它能帮助您使用
PopupWindow
。它会让你对定位和内容有更多的控制。我尝试过使用弹出窗口,但我无法根据项目的位置更改弹出窗口的位置。如果项目是最右边的项目,则弹出窗口应显示在单击点的顶部。但是我无法使用showAtLocation(查看视图不活动、重力模式、int-xOffset、int-yOffset):我已经用弹出窗口发布了我的代码。但这与Play Store不同。请检查我的第一个代码。我试过弹出菜单。它在底部或顶部显示菜单。但我希望菜单位于右下角,或者如果单击了最右边的项目,它应该与底部对齐,但朝屏幕中心打开。实际上,弹出菜单根据房间(空间)显示其菜单它的菜单我希望它可以帮助你无论如何尝试谷歌你可以得到你的答案快乐编码我搜索过谷歌,但找不到任何解决方案
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="@+id/spam"
android:title="Spam"/>
<item
android:id="@+id/blockuser"
android:title="Block User"/>
<item
android:id="@+id/remove"
android:title="Remove"/>
PopupMenu popup = new PopupMenu(activity, v);
/** Adding menu items to the popumenu */
popup.getMenuInflater().inflate(R.menu.main, popup.getMenu());
popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.spam:
Toast.makeText(activity, "Spam clicked",
Toast.LENGTH_SHORT).show();
break;
case R.id.blockuser:
Toast.makeText(activity, " Block user clicked",
Toast.LENGTH_SHORT).show();
break;
case R.id.remove:
Toast.makeText(activity, "Remove clicked",
Toast.LENGTH_SHORT).show();
// RecentFragment rf = new RecentFragment();
// rf.onItemClick(position);
break;
default:
break;
}
return false;
}
});
popup.show();