簡體   English   中英

以編程方式設置單選按鈕樣式

[英]Radio Button style programmatically

我想在一個片段中動態地創建一些單選按鈕,我只有樣式問題。 如果我將單選按鈕代碼放在 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>

結果

在此處輸入圖片說明

爪哇代碼

這段代碼放在片段中的 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);

    }

}

結果

在此處輸入圖片說明

如您所見,單選按鈕具有不同的樣式,如果可能,有人可以幫助我以編程方式應用默認樣式嗎?

您必須根據需要在 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); 

我認為,這將幫助您在 Radio Button 中實現自定義樣式。

謝謝Dharma,我按照你的建議,改變了一些東西,我解決了!

爪哇代碼

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 單選按鈕選擇器

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

使用 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_button.xml

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

樣式文件

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

作者:烏比拉哈拉(墨西哥)

以編程方式,我建議為循環中的每個單選按鈕設置一個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定義未選中按鈕(藍色)的顏色。

我個人認為這是一個更好的解決方案,因為它比在代碼庫中的其他地方創建樣式和其他依賴項更簡潔。 盡可能使用最簡潔有效的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM