开发者

Any way to change the color of a radio button?

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 you s开发者_高级运维elect it. It seems to always default to some bright green color. Is this something that is editable or no?

Thanks


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜