2016-07-05 66 views
0

我想更改RadioButton的實際圓圈。如何以編程方式設置RadioButton Android Circle的顏色

我已經看遍了所有的StackOverflow,似乎沒有任何工作。我正在使用API​​ 17,不能使用ColorStateList。自定義drawables似乎把我們的外觀和感覺的東西。

我想要改變按鈕提示的效果,但我需要以編程方式。

我相信我必須請你錯過一件非常簡單的事情!

+0

的可能的複製[無法改變Android的單選按鈕的顏色(http://stackoverflow.com/questions/28307512/cant-change-radio-button-color-on-android) – Sufian

回答

1

如何以編程設置的單選按鈕的Android

那可真是非特異性的顏色,所以這裏是最相關的,我發現:

RadioButton rad;//initialize first! 
//You can set the background color 
rad.setBackgroundColor(Color.BLUE); 
//Text color 
rad.setTextColor(Color.WHITE); 
//or highlight color 
rad.setHighlightColor(Color.GREEN); 

高亮顯示顏色的當按下並按住RadioButton時出現的顏色(默認爲黃色ish)

編輯:

在單選按鈕的初始化,稱其爲AppCompatRadioButton代替

AppCompatRadioButton rad = .... 
rad.setHighlightColor(Color.GREEN); 

從返工:https://stackoverflow.com/a/32472971/6296561

編輯

試試這個:

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context=".MainActivity" 
    tools:showIn="@layout/app_bar_main"> 

    <RadioGroup 
     android:id="@+id/radioGroup" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 


    </RadioGroup> 

</RelativeLayout> 

MainActivity.java

public class MainActivity extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 


     RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup); 

     /** 
     * First Radio Button 
     */ 
     RadioButton RB1= (RadioButton) getLayoutInflater().inflate(R.layout.custom_radiobutton, null);//initialize and set content 
     RB1.setText("RB1"); 
     radioGroup.addView(RB1);//add the radiobutton to the radiogroup defined in the layout 

     /** 
     * Second Radio Button 
     */ 
     RadioButton RB2 = (RadioButton) getLayoutInflater().inflate(R.layout.custom_radiobutton, null);//initialize and set content 
     RB2.setText("RB2"); 
     radioGroup.addView(RB2);//add the radiobutton to the radiogroup defined in the layout 
    } 
} 

custom_radiobutton.xml

<?xml version="1.0" encoding="utf-8"?> 
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:buttonTint="@color/colorPrimary" 
    android:text=""> 
<!-- leave empty --> 
</RadioButton> 

注:buttonTint只能在API 21+。 (未測試),您可以將RadioButton更改爲AppCompatRadioButton。 (這是未經測試,所以我不知道這是否適用於API 20,下)

<?xml version="1.0" encoding="utf-8"?> 
<AppCompatRadioButton xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:buttonTint="@color/colorPrimary" 
    android:text=""> 
<!-- leave empty --> 
</AppCompatRadioButton> 

Documentation about AppCompatRadioButton

如果使用AppCompatRadioButton,我想你也必須使用AppCompatRadioGroup和編輯的ACRB的創作於:

AppCompatRadioButton RB1 = (AppCompatRadioButton) getLayoutInflater().inflate(R.layout.custom_radiobutton, null);//initialize and set content 
RB1.setText("RB1"); 
radioGroup.addView(RB1);//add the radiobutton to the radiogroup defined in the layout 
+0

對不起,你是對的我沒有具體。我編輯它。我想改變圓圈的顏色,所以不幸的是你的答案不工作 – SuperBale

+0

更新了問題。這對你有幫助嗎? – Zoe

+0

@color可以用#代替,顏色代碼我認爲 – Zoe

相關問題