Java 如何在自定义弹出菜单中设置右侧图标
单击按钮,我将显示包含语言列表的菜单。现在我希望图标显示在菜单中每个项目标题的右侧。我搜索了一下,但没有找到合适的解决方案,请帮我解决 menu_language.xml:Java 如何在自定义弹出菜单中设置右侧图标,java,android,popupmenu,country-codes,menu-items,Java,Android,Popupmenu,Country Codes,Menu Items,单击按钮,我将显示包含语言列表的菜单。现在我希望图标显示在菜单中每个项目标题的右侧。我搜索了一下,但没有找到合适的解决方案,请帮我解决 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-
<?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" />
现在图标在菜单的左边,但我希望它在标题的右边。请帮助,有什么线索吗?对于不能使用菜单的自定义布局,另一个选项是 这有助于显示带有自定义视图的任意窗口,您可以对其设置样式并将其用作菜单 做这样的事
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;
}
<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>
像这样创建一个xml文件
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;
}
<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>
hie murphy!您正在使用内置菜单xml文件来创建弹出菜单!默认情况下,menuIcon always on left属性可以根据屏幕大小展开右侧的标签。您可以这样做!您只需在“菜单”的“布局”文件夹中设计一个自定义布局文件即可!然后你得到菜单充气器,让它调零旧菜单,并在那里添加你的自定义布局?想要一个代码示例吗?或者简单地说,在longPress上弹出的contexrMenu有选项可供您显示所需的所有内容!但是如果你坚持使用optionMenu,你必须使用我的解决方案。尝试this@Rizwanatta谢谢你们,是的,若你们分享这个例子会更清楚。为什么我们需要xml文件?我需要在哪里为菜单充气?你不能自定义菜单文件,它是预先构建的!但是,您可以做的只是使一个定制的设计菜单选项类似于xml文件,并将其膨胀!但我喜欢它的数千个项目,所以在这种方式下,它将非常大,更好的选择,这将是像有一个弹出式窗口中打开一个微调器!有语言名称和图像!您可以从strings.xml中的字符串数组或者使用java来填充它。是的,但是菜单的右边有图标吗?人类唯一的标志我们必须改变为什么这么难。