【问题标题】:How can i align the text of the radio button to the left of the button如何将单选按钮的文本对齐到按钮的左侧
【发布时间】:2017-09-22 16:47:54
【问题描述】:

如何将文本对齐到单选按钮数组的左侧?

       RadioButton[] rb = new RadioButton[answerOption.size()];

       for (int i = 0; i < answerOption.size(); i++) {
        rb[i] = new RadioButton(getContext());
        rb[i].setButtonTintList(ColorStateList.valueOf(Color.BLUE));
        rb[i].setTextColor(Color.BLACK);
        rg.addView(rb[i]);
        rb[i].setText(i);

    }

【问题讨论】:

    标签: android radio-button


    【解决方案1】:

    试试这个

    android:button="@null"
    android:drawableRight="@android:drawable/btn_radio"
    

    你也可以试试

    android:layoutDirection="rtl"
    

    【讨论】:

    • radio 组的第四个元素被移到第一个......所以索引改变了
    • 不,不会发生
    • 试试这个 android:layoutDirection="rtl" android:textAlignment="textStart" android:layout_gravity="start"
    • 如果我在 xml 中声明了单选按钮,但我只有一个单选组
    【解决方案2】:

    试试这个代码

    在 Xml 上

    android:layoutDirection="rtl"
    android:textAlignment="textStart"
    android:layout_gravity="start" 
    

    更改此代码 Java 文件

    int i;
    RadioButton[] rb = new RadioButton[answerOption.size()];
    for (i = 0; i < answerOption.size(); i++) {
       rb[i] = new RadioButton(getContext());    
    }
    rb[i].setButtonTintList(ColorStateList.valueOf(Color.BLUE));
    rb[i].setTextColor(Color.BLACK);
    rb[i].setText(i);
    rg.addView(rb[i]);
    

    【讨论】:

    • addView方法被调用了两次,所以抛出异常
    • 删除最后一行 rg.addView(rb[i]);
    • 它可以工作,但我希望单选按钮的索引不变,因为它将最后一个元素的索引更改为第一个元素
    • 使用这个 Collections.sort(answerOption);在上面的 int i
    • 你是对的,但我想要索引而不是按钮的值