[英]Android - How to add Radio Buttons to a Navigation Drawer
I'm trying to add some radio buttons to a Navigation Drawer. 我正在尝试向导航抽屉中添加一些单选按钮。 This is what I've been trying so far : activity_main_drawer.xml
到目前为止,这是我一直在尝试的方法: activity_main_drawer.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:showIn="navigation_view">
<group
android:id="@+id/radio_button_group"
android:checkableBehavior="single"
android:visible="true">
<item
android:id="@+id/radio_buttonX-axis"
android:icon="@drawable/ic_x_axis"
android:title="Show x-axis"
app:actionViewClass="android.widget.RadioButton"/>
<item
android:id="@+id/radio_buttonY-axis"
android:icon="@drawable/ic_y_axis"
android:title="Show y-axis"
app:actionViewClass="android.widget.RadioButton" />
<item
android:id="@+id/radio_buttonZ-axis"
android:icon="@drawable/ic_z_axis"
android:title="Show z-axis"
app:actionViewClass="android.widget.RadioButton" />
</group>
</menu>
Reference this and this answers. 参考这个和这个答案。
Is this even possible? 这有可能吗?
int[] ids = {R.id.radio_buttonX_axis, R.id.radio_buttonY_axis, R.id.radio_buttonZ_axis};
RadioGroup radioGroup = new RadioGroup(navigationView.getContext());
radioGroup.setOrientation(RadioGroup.VERTICAL);
for (int id : ids) {
MenuItem menuItem = navigationView.getMenu().findItem(id);
RadioButton radioButton = (RadioButton) menuItem.getActionView();
radioGroup.addView(radioButton);
}
But it gives an error of : 但是它给出了一个错误:
The specified child already has a parent.
指定的孩子已经有一个父母。 You must call
removeView()
on the child's parent first.您必须先在孩子的父母上调用
removeView()
。
But I wasn't able to figure out how to call it 但是我不知道怎么称呼它
navigationView.getMenu().removeGroup(R.id.radio_button_group);
RadioGroup radioGroup = new RadioGroup(navigationView.getContext());
for (int i = 0; i < 3; i++) {
RadioButton radioButton = new RadioButton(navigationView.getContext());
radioButton.setText("Show X - Axis");
radioGroup.addView(radioButton);
}
navigationView.addView(radioGroup);
But it doesn't works as expected (See Image). 但这并不能按预期工作(请参见图片)。
I tried to add the radioGroup
to Menu
, but I couldn't figure out how to do it either. 我试图将
radioGroup
添加到Menu
,但是我也不知道该怎么做。 This is what I was trying to do : 这就是我想要做的:
Menu testMenu = navigationView.getMenu();
testMenu.add(R.id.radio_button_group, testMenu.findItem(R.id.radio_buttonX_axis).getActionView().getId(), 1, "Show X - Axis");
But it just adds an option to NavigationDrawer
, not a radio button. 但这只是向
NavigationDrawer
添加一个选项,而不是单选按钮。
This is just hypothetical, I don't know if it's even possible or not. 这只是假设,我不知道是否可能。
radio_buttons
and store in some RadioButton[]
array. radio_buttons
的实例,并将其存储在某些RadioButton[]
数组中。 RadioGroup
, and add all the RadioButtons[]
to it. RadioGroup
,并将所有RadioButtons[]
添加到其中。 RadioGroup
to navigationView
RadioGroup
添加到navigationView
But none has been helpful. 但没有一个有帮助。 Please tell me what to do?
请告诉我应该怎么做?
You can add a radiobuttons in the menu drawer. 您可以在菜单抽屉中添加一个单选按钮。 First create an xml layout (radio_button.xml):
首先创建一个xml布局(radio_button.xml):
<RadioGroup android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RadioButton" />
</RadioGroup>
Then add it to the drawer: 然后将其添加到抽屉中:
<item android:title="Radios">
<menu>
<item
app:actionLayout="@layout/radio_button"
android:id="@+id/nav_radios"
android:title=""
/>
</menu>
</item>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.