简体   繁体   English

以编程方式设置单选按钮样式

[英]Radio Button style programmatically

I would to create a number of radio button dinamically in a fragment, I only have problem with style.我想在一个片段中动态地创建一些单选按钮,我只有样式问题。 If I put radiobutton code in the xml file, default style is applied correctly, but when I create radiobutton through a function I see different style!如果我将单选按钮代码放在 xml 文件中,默认样式会正确应用,但是当我通过函数创建单选按钮时,我会看到不同的样式!

XML XML

<RadioGroup
            android:id="@+id/radiogroup"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical"
            android:animationCache="false">

            <RadioButton
                android:text="RadioButton 1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/radioButton3" />

            <RadioButton
                android:text="RadioButton 2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/radioButton4" />


</RadioGroup>

RESULT结果

在此处输入图片说明

JAVA CODE爪哇代码

This code is put in onCreateView in the fragment这段代码放在片段中的 onCreateView 中

public void addRadioButton(Context ctx,int num){

    RadioGroup radioGroup= (RadioGroup) alertInflatedView.findViewById(R.id.radiogroup);

    for (int i = 1; i <= num; i++) {
        RadioButton radioButton  = new RadioButton(ctx);
        radioButton.setId(1+i);
        radioButton.setText("Radio " + radioButton.getId());
        radioButton.setTextColor(getResources().getColor(R.color.black));

        radioGroup.addView(radioButton);

    }

}

RESULT结果

在此处输入图片说明

As you can see radio buttons have different style, someone could help me, if is possibile, to apply default style programmatically?如您所见,单选按钮具有不同的样式,如果可能,有人可以帮助我以编程方式应用默认样式吗?

you have to create style on drawable or style.xml, as your requirement.您必须根据需要在 drawable 或 style.xml 上创建样式。

drawable/null_selector.xml drawable/null_selector.xml

  <?xml version="1.0" encoding="utf-8"?> 
  <selector xmlns:android="http://schemas.android.com/apk/res/android" > 
         <item android:drawable="@android:color/transparent" /> 
  </selector> 

Set each button to use it (and to center the text) like this (R.drawable.null_selector is selector XML):像这样设置每个按钮以使用它(并使文本居中)(R.drawable.null_selector 是选择器 XML):

Now, In your Activity, you must be implement such style.现在,在您的 Activity 中,您必须实现这种风格。

  RadioButton radioButton = new RadioButton(ctx);
  radioButton.setText(Integer.toString(i));
  radioButton.setGravity(Gravity.CENTER); 
  radioButton.setButtonDrawable(R.drawable.null_selector); 

I think, this will help you for implementing custom style in Radio Button.我认为,这将帮助您在 Radio Button 中实现自定义样式。

Thanks Dharma, I followed your suggestion, changing something, and I solved!谢谢Dharma,我按照你的建议,改变了一些东西,我解决了!

JAVA CODE爪哇代码

public void addRadioButton(Context ctx,int num){

    RadioGroup radioGroup= (RadioGroup) alertInflatedView.findViewById(R.id.radiogroup);
    RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(
            RadioGroup.LayoutParams.MATCH_PARENT,
            RadioGroup.LayoutParams.WRAP_CONTENT);

    for(int i=0; i<num; i++){

        RadioButton radioButton  = new RadioButton(ctx);
        radioButton.setId(1+i);
        radioButton.setText("Radio"+i);
        radioButton.setTextSize(16);
        radioButton.setTextColor(getResources().getColor(R.color.black));
        radioButton.setButtonDrawable(R.drawable.radio_button_selector);
        radioButton.setPadding(80,0,0,0);
        radioButton.setGravity(Gravity.CENTER_VERTICAL);
        radioButton.setLayoutParams(layoutParams);
        radioGroup.addView(radioButton);

    }

}

XML RADIO BUTTON SELECTOR with checked and unchecked button image带有选中和未选中按钮图像的 XML 单选按钮选择器

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="false" android:drawable="@drawable/unchekedradiobutton" />
<item android:state_checked="true" android:drawable="@drawable/checkedradiobutton" />
<item android:drawable="@drawable/unchekedradiobutton" /> <!-- default -->

Use an Inflater instance to inflate a custom layout and easily get a custom Radiobutton使用 Inflater 实例来膨胀自定义布局并轻松获得自定义 Radiobutton

private RadioButton createCustomRadioButton(Context context){
    LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(R.layout.radio_button,null);
    RadioButton radioButton  = (RadioButton) v.findViewById(R.id.radio_button);
    radioButton.setText("It Works!");

    ((ViewGroup)radioButton.getParent()).removeView(radioButton);
    return radioButton;
}

radio_buttom.xml radio_button.xml

<RadioButton
    android:id="@+id/radio_button"
    style="@style/radio"
    android:background="@drawable/style_line" />

style.xml样式文件

<style name="radio">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_marginLeft">30dp</item>
    <item name="android:layout_marginRight">30dp</item>
    <item name="android:layout_marginTop">10dp</item>
    <item name="android:layout_marginBottom">10dp</item>
    <item name="android:padding">10dp</item>
    <item name="android:drawablePadding">5dp</item>
    <item name="android:textColor">@color/whiteColor</item>
    <item name="android:textColorHint">@color/hintColor</item>
    <item name="android:editTextColor">@color/whiteColor</item>
</style>

by Ubirajara (México)作者:乌比拉哈拉(墨西哥)

Programmatically, I recommend setting a ColorStateList to each radio button in the loop like this: radioButton.setButtonTintList(getRadioButtonColors());以编程方式,我建议为循环中的每个单选按钮设置一个ColorStateList ,如下所示: radioButton.setButtonTintList(getRadioButtonColors());

Then然后

private ColorStateList getRadioButtonColors() {
        return new ColorStateList (
                new int[][] {
                        new int[] {android.R.attr.state_checked}, // checked
                        new int[] {android.R.attr.state_enabled} // unchecked
                },
                new int[] {
                        Color.GREEN, // checked
                        Color.BLUE   // unchecked
                }
        );
    }

Where android.R.attr.state_checked defines the color for the checked button (green) and android.R.attr.state_enabled defines the color for unchecked buttons (blue) .其中android.R.attr.state_checked定义选中按钮的颜色(绿色)android.R.attr.state_enabled定义未选中按钮(蓝色)的颜色。

Personally I think this is a better solution because it's more concise than creating styles and other dependencies elsewhere in the codebase.我个人认为这是一个更好的解决方案,因为它比在代码库中的其他地方创建样式和其他依赖项更简洁。 Whenever possible use the most concise and efficient approach.尽可能使用最简洁有效的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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