Android 如何在弹出菜单组的左侧显示单选按钮图标?
我在弹出菜单中有一个单选按钮组:Android 如何在弹出菜单组的左侧显示单选按钮图标?,android,android-radiobutton,Android,Android Radiobutton,我在弹出菜单中有一个单选按钮组: <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:id="@+id/mode_group" android:checkableBehavior="single"> <item android:i
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="@+id/mode_group"
android:checkableBehavior="single">
<item android:id="@+id/red"
android:title="Red" />
<item android:id="@+id/white"
android:title="White" />
<item android:id="@+id/blue"
android:title="Blue" />
</group>
</menu>
它在标题右侧显示图标:
是否可以将其配置为在左侧显示图标?您可以使用反射来实现这一点 java代码
Context contextWrapper = new ContextThemeWrapper(MainActivity.this, R.style.PopupStyle);
PopupMenu popup = new PopupMenu(contextWrapper,v);
/* The below code in try catch is responsible to display icons*/
try {
Field[] fields = popup.getClass().getDeclaredFields();
for (Field field : fields) {
if ("mPopup".equals(field.getName())) {
field.setAccessible(true);
Object menuPopupHelper = field.get(popup);
Class<?> classPopupHelper = Class.forName(menuPopupHelper.getClass().getName());
Method setForceIcons = classPopupHelper.getMethod("setForceShowIcon", boolean.class);
setForceIcons.invoke(menuPopupHelper, true);
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
popup.getMenuInflater().inflate(R.menu.pop_up, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this,
"Clicked popup menu item " + item.getTitle(),
Toast.LENGTH_SHORT).show();
return true;
}
});
popup.show();
Context-contextWrapper=new-ContextThemeWrapper(main-activity.this,R.style.PopupStyle);
PopupMenu popup=新的PopupMenu(contextWrapper,v);
/*try-catch中的以下代码负责显示图标*/
试一试{
Field[]fields=popup.getClass().getDeclaredFields();
用于(字段:字段){
if(“mPopup.equals(field.getName())){
字段。setAccessible(true);
Object menuPopupHelper=field.get(弹出窗口);
Class CLASSPOPUSCURLPER=Class.forName(menuPopupHelper.getClass().getName());
方法setForceIcons=classpopunculper.getMethod(“setForceShowIcon”,boolean.class);
调用(menuPopupHelper,true);
打破
}
}
}捕获(例外e){
e、 printStackTrace();
}
popup.getMenuInflater().充气(R.menu.pop_up,popup.getMenu());
setOnMenuItemClickListener(新的PopupMenu.OnMenuItemClickListener(){
公共布尔onMenuItemClick(菜单项){
Toast.makeText(MainActivity.this,
“单击的弹出菜单项”+项.getTitle(),
吐司。长度(短)。show();
返回true;
}
});
popup.show();
弹出.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/red"
android:icon="@android:drawable/presence_online"
android:title="Red" />
<item android:id="@+id/white"
android:icon="@android:drawable/presence_online"
android:title="White" />
<item android:id="@+id/blue"
android:icon="@android:drawable/presence_online"
android:title="Blue" />
</menu>
<style name="PopupStyle" parent="Widget.AppCompat.PopupMenu">
<item name="android:textColorPrimary">@color/colorAccent</item>
</style>
如果你想改变菜单样式
在styles.xml中
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/red"
android:icon="@android:drawable/presence_online"
android:title="Red" />
<item android:id="@+id/white"
android:icon="@android:drawable/presence_online"
android:title="White" />
<item android:id="@+id/blue"
android:icon="@android:drawable/presence_online"
android:title="Blue" />
</menu>
<style name="PopupStyle" parent="Widget.AppCompat.PopupMenu">
<item name="android:textColorPrimary">@color/colorAccent</item>
</style>
@颜色/颜色重音
或者,如果您想创建自定义布局检查,check@MohammedAlaa,thx,它会将单选按钮放在左侧,但文本不会立即跟随按钮,因此按钮是左对齐的,文本是右对齐的,中间有很大的间隙。比如[bt xxxx space xxxx Red],寻找像[bt.Red xxxx space xxxx]这样的东西。这是您想要实现的吗?是的,但是
android:supportsRtl=“true”
。按下左侧的按钮和最右侧的文本