简体   繁体   English

Android-如何将单选按钮添加到导航抽屉

[英]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. 参考这个这个答案。

Method 1: Add Radio Group after the Radio Buttons are created 方法1:创建单选按钮后,添加单选组

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 但是我不知道怎么称呼它

Method 2: Add Radio Buttons to Navigation Drawer dynamically 方法2:将单选按钮动态添加到导航抽屉

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添加一个选项,而不是单选按钮。

Method 3 方法3

This is just hypothetical, I don't know if it's even possible or not. 这只是假设,我不知道是否可能。

  1. Copy the instances of all radio_buttons and store in some RadioButton[] array. 复制所有radio_buttons的实例,并将其存储在某些RadioButton[]数组中。
  2. Remove Group (as done in Method 2) 删除组(如方法2中所述)
  3. Create a RadioGroup , and add all the RadioButtons[] to it. 创建一个RadioGroup ,并将所有RadioButtons[]添加到其中。
  4. Add this new 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM