【问题标题】:Radio Button style programmatically以编程方式单选按钮样式
【发布时间】:2021-04-02 05:41:56
【问题描述】:

我想在一个片段中动态地创建一些单选按钮,我只有样式问题。如果我将单选按钮代码放在 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>

结果

JAVA 代码

这段代码放在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);

    }

}

结果

您可以看到单选按钮具有不同的样式,如果可能的话,有人可以帮助我以编程方式应用默认样式吗?

【问题讨论】:

标签: android android-radiobutton


【解决方案1】:

您必须根据需要在 drawable 或 style.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> 

将每个按钮设置为使用它(并使文本居中),如下所示(R.drawable.null_selector 是选择器 XML):

现在,在你的 Activity 中,你必须实现这种风格。

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

我认为,这将有助于您在单选按钮中实现自定义样式。

【讨论】:

    【解决方案2】:

    感谢Dharma,我听从了你的建议,改变了一些东西,我解决了!

    JAVA 代码

    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

    <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 -->
    

    【讨论】:

      【解决方案3】:

      使用 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

      <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>
      

      Ubirajara(墨西哥)

      【讨论】:

        【解决方案4】:

        以编程方式,我建议为循环中的每个单选按钮设置一个 ColorStateList,如下所示: radioButton.setButtonTintList(getRadioButtonColors());

        然后

        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
                        }
                );
            }
        

        android.R.attr.state_checked 定义选中按钮 (绿色) 的颜色,android.R.attr.state_enabled 定义未选中按钮 (蓝色) 的颜色。

        我个人认为这是一个更好的解决方案,因为它比在代码库的其他地方创建样式和其他依赖项更简洁。尽可能使用最简洁有效的方法。

        【讨论】:

          猜你喜欢
          • 2014-09-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多