在Android API 19中,添加了PopupMenu的构造函数,允许指定gravity。
public PopupMenu (Context context, View anchor, int gravity)
我该如何在旧版本中设置gravity?
在Android API 19中,添加了PopupMenu的构造函数,允许指定gravity。
public PopupMenu (Context context, View anchor, int gravity)
我该如何在旧版本中设置gravity?
将代码行中的 import android.widget.PopupMenu;
改为 import android.support.v7.widget.PopupMenu;
androidx
,则请使用import androidx.appcompat.widget.PopupMenu;
,它将起作用。 - Vucko您可以使用RTL来将重心放在右侧这里是链接
RTL支持Android 4.2及以上版本
或者您可以创建自定义的弹出菜单并进行填充。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
popup = new PopupMenu(context, arg1,Gravity.CENTER );
//popupWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, xPos, yPos);
}
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.listmenu, popup.getMenu());
showPopup(viewOfWherePopupmenuShows,popmenuNameList.size());
PopupMenu popMenu = new PopupMenu(Activity.this,v)
int size =popmenuNameList.size();
for(int i =0; i <size; i++){
popMenu.getMenu().add(popmenuNameList.get(i)).setIcon(R.drawable.logo);
}
popMenu.show();
protected void showPopupMenu(View v, int size){
//create instance
PopupMenu popup = new PopupMenu(Activity.this,v);
//inflating the popup using xml
popup.getMenuInflater().inflate(R.menu.menu_popup_list),popup.getMenu());
}
<style name="style" parent="android:Theme.Holo.Light">
<item name="android:popupMenuStyle">...</item>
<item name="android:popupAnimationStyle">...</item>
<item name="android:popupBackground">...</item>
</style>