在单击按钮时,我将显示包含语言列表的菜单。现在,我希望图标显示在菜单中每个项目的标题的右边。我谷歌了但没有找到合适的解决方案,请帮助我。
menu_language.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/nav_arabic"
android:title="Arabic" />
<item
android:id="@+id/nav_southAfrica"
android:icon="@drawable/ic_earphones"
android:title="Afrikaans"
/>
<item
android:id="@+id/nav_albania"
android:icon="@drawable/ic_earphones"
android:title="Albanian " />
<item
android:id="@+id/nav_armenian"
android:title="Armenian" />
<item
android:id="@+id/nav_azerbaijani"
android:icon="@drawable/ic_earphones"
android:title="Azerbaijani" />
<item
android:id="@+id/nav_bangla"
android:icon="@drawable/ic_earphones"
android:title="Bangla" />
<item
android:id="@+id/nav_basque"
android:icon="@drawable/ic_earphones"
android:title="Basque" />
<item
android:id="@+id/nav_belarusian"
android:title="Belarusian" />Language.java:
public void showMenu(View v) {
PopupMenu popup = new PopupMenu(getActivity(), v);
Object menuHelper;
Class[] argTypes;
try {
Field fMenuHelper = PopupMenu.class.getDeclaredField("mPopup");
fMenuHelper.setAccessible(true);
menuHelper = fMenuHelper.get(popup);
argTypes = new Class[]{boolean.class};
menuHelper.getClass().getDeclaredMethod("setForceShowIcon", argTypes).invoke(menuHelper, true);
} catch (Exception e) {
}
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu_language, popup.getMenu());
popup.setOnMenuItemClickListener(this);到目前为止,图标在菜单的左边,但我希望它在标题的右边。帮帮忙,有线索吗?
发布于 2018-07-24 12:37:31
对于自定义布局,您不能使用菜单,另一个选项是PopupWindow,它帮助您使用自定义视图显示任意窗口,您可以将其样式化并将其用作菜单。
做这样的事
PopupWindow popupwindow_obj = popupDisplay();
popupwindow_obj.showAsDropDown(clickbtn, -40, 18); // where u want show on view click event popupwindow.showAsDropDown(view, x, y);
public PopupWindow popupDisplay()
{
final PopupWindow popupWindow = new PopupWindow(this);
// inflate your layout or dynamically add view
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.mylayout, null);
TextView item = (TextView) view.findViewById(R.id.button1);
popupWindow.setFocusable(true);
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(view);
return popupWindow;
}创建这样的xml文件
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/button1"
drawableRight='@drawbale/icon'
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Window test" />
</LinearLayout>https://stackoverflow.com/questions/51496350
复制相似问题