无法将RadioButton颜色更改为黑色

时间:2016-05-20 18:35:36

标签: java android xml radio-button

我正在尝试更改我的RadioButton颜色。我以编程方式添加了RadioButtons,但是在研究之后,我无法将颜色更改为单击时带有黑色填充的纯黑色边框。这是我添加按钮的代码,我知道可能需要考虑两个API级别< 21和> 21:

 public void addRadioButtonsWithFirebaseAnswers(DataSnapshot dataSnapshot, int numberOfAnswers) {
    mPollAnswerArrayList = new ArrayList<RadioButton>();
    for (int i = 0; i < numberOfAnswers; i++) {
        Log.e("Number of Answers", "The number of answers is " + numberOfAnswers);
        mPollAnswerArrayList.add(i, new RadioButton(getActivity().getApplicationContext()));
        mPollAnswerArrayList.get(i).setId(i);
        String firebaseChild = String.valueOf(i + 1);
        mPollAnswerArrayList.get(i).setText(dataSnapshot.child(POLL_ANSWERS_LABEL).child(firebaseChild).child("Answer").getValue().toString());
        mPollAnswerArrayList.get(i).setTextColor(getResources().getColor(R.color.black));

        mPollAnswerArrayList.get(i).setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.radio_button_answer_text_size));
        mParams.setMargins((int) getResources().getDimension(R.dimen.radio_question_margin_left), 0, 0, (int) getResources().getDimension(R.dimen.radio_question_margin_bottom));

        mPollQuestionRadioGroup.addView(mPollAnswerArrayList.get(i), mParams);
    }
}

enter image description here

2 个答案:

答案 0 :(得分:0)

您可以使用选择器

更改所选单选按钮的颜色

<强> selector_radio_btn / XML

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
      <item android:state_checked="true" android:drawable="@color/checkbox_active" />
      <item android:state_checked="false" android:drawable="@color/checkbox_inactive" />
</selector>

<强> checkbox_inactive.xml

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >
    <solid
        android:color="@android:color/transparent" />
    <stroke
        android:width="1dp"
        android:color="#53aade" />
</shape>

<强> checkbox_activi.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval" >
            <solid android:color="@android:color/transparent" />
            <stroke
                android:width="1dp"
                android:color="#53aade" />
        </shape>
    </item>
    <item left="5dp" right="5dp" top="5dp" bottom="5dp">
        <shape android:shape="oval" >
            <solid android:color="#53aade" />
        </shape>
    </item>
</layer-list>

在radiobutton中设置selector_radio_btn.xml

mPollAnswerArrayList.get(i).setButtonDrawable(getResources().getDrawable(R.drawable.selector_radio_btn));

答案 1 :(得分:0)

解决API&lt; 21和API&gt; 21,我做了以下事情:

 if (Build.VERSION.SDK_INT >= 21) {
            mPollAnswerArrayList.get(indexCreated).setButtonTintMode(PorterDuff.Mode.DARKEN);
        } else {
            mPollAnswerArrayList.get(indexCreated).setButtonDrawable(R.drawable.black_ring);

        }

我还添加了drawable,如下所示:

我的drawable文件夹中的

black_ring.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadiusRatio="3"
    android:shape="ring"
    android:thickness="3dp"
    android:useLevel="false" >

    <solid android:color="@color/black" />

    <size
        android:height="25dp"
        android:width="25dp" />

</shape>