单击按钮后,我将显示包含语言列表的菜单。现在,我希望图标显示在菜单中每个项目标题的右侧。我用谷歌搜索,但找不到合适的解决方案,请帮帮我。
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);
到目前为止,该图标位于菜单的左侧,但我希望它位于title的右侧。请帮忙,有任何线索吗?
答案 0 :(得分:0)
对于自定义布局,您不能使用菜单,另外一个选择是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>