Any way to change the color of a radio button?

前端 未结 3 574
梦如初夏
梦如初夏 2020-12-17 17:21

I\'m working on an android form with a radio group containing a set of radio buttons. From what I can tell there is no way to set the color a radio button highlights when yo

相关标签:
3条回答
  • 2020-12-17 18:01

    Use AppCompatRadioButton instead of RadioButton.

      <android.support.v7.widget.AppCompatRadioButton
            android:id="@+id/rb"
            app:buttonTint="@color/colorAccent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    

    To change the color programatically do this:

    ColorStateList colorStateList = new ColorStateList(
                    new int[][]{
                            new int[]{android.R.attr.state_enabled} //enabled
                    },
                    new int[] {getResources().getColor(R.color.colorPrimary) }
            );
    
    AppCompatRadioButton radioButton = (AppCompatRadioButton) findViewById(R.id.rb);
    radioButton.setSupportButtonTintList(colorStateList);
    
    0 讨论(0)
  • 2020-12-17 18:05

    On api level 21+ you can change the buttonTint

    <RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/myId"
    android:checked="true"
    android:buttonTint="@color/accent"/>
    
    0 讨论(0)
  • 2020-12-17 18:11

    Yes you can create your own drawable for what you want it to look like when checked and use android:button to set it to the resource.

    Example here

    0 讨论(0)
提交回复
热议问题