2

单击按钮时,我将显示包含语言列表的菜单。现在我希望图标显示在菜单中每个项目的标题右侧。我用谷歌搜索但没有找到合适的解决方案,请帮助我。

菜单语言.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" />

语言.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);

到目前为止,该图标位于菜单的左侧,但我希望它位于标题的右侧。请帮忙,有什么线索吗?

4

1 回答 1

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>
于 2018-07-24T12:37:31.873 回答