2010-04-26 76 views
11

我正在處理一個包含一組單選按鈕的收音機組的Android窗體。從我所能告訴的是,當您選擇它時無法設置單選按鈕突出顯示的顏色。它似乎總是默認爲一些明亮的綠色。這是可編輯或不可編輯的東西嗎?任何方式來改變單選按鈕的顏色?

感謝

+0

我認爲這是一個利特爾有點晚,給你一個ansewr,但你可以檢查我的ansewr這個問題:HTTP://計算器。 com/a/35610511/1663453 – Sierisimo 2016-02-24 18:49:32

回答

9

是的,你可以創建自己繪製你希望它看起來選中時喜歡和使用Android的:按鈕,將其設置爲資源。

Example here

0

在API級別21+,您可以更改buttonTint

<RadioButton 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:id="@+id/myId" 
android:checked="true" 
android:buttonTint="@color/accent"/> 
+0

而在API小於21或使用兼容性:http://stackoverflow.com/a/35610511/1663453 – Sierisimo 2016-02-24 18:49:57

1

使用AppCompatRadioButton,而不是單選按鈕。

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

要更改顏色以編程方式做到這一點:

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