Android自定义复选框/单选按钮文本未显示

时间:2014-03-31 03:05:59

标签: android android-checkbox android-radiogroup android-radiobutton

我正在尝试使用来自Http请求的信息以编程方式创建自定义RadioButtons / Checkbox(在应用内创建调查)。我在Android Checkbox text not displaying中尝试了建议的解决方案,但文字仍然没有出现。

我可以创建自定义按钮视图,但由于某种原因,文本不会显示。知道我哪里错了吗?

checkbox_selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/multiselected" android:state_checked="true"/>
    <item android:drawable="@drawable/multiunselected" android:state_checked="false"/>

</selector>

radio_selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/singleselected" android:state_checked="true"/>
    <item android:drawable="@drawable/singleunselected" android:state_checked="false"/>

</selector>

以下是我创建它们的方式:

if (mQuestion != null && mQuestion.isSingleResponse()
                    && mAnswerTitles.size() > 0) {
                for (int i = 0; i < mAnswerTitles.size(); i++) {
                    Answer answer1 = mAnswerTitles.get(i);
                    RadioButton answer = new RadioButton(getActivity());

                    answer.setText(answer1.getTitle().toString());
                    answer.setTextSize(24);
                    answer.setTextColor(Color.BLACK);
                    answer.setId(answer1.getId());
                    answer.setButtonDrawable(R.drawable.radio_selector);
                    answer.setLayoutParams(mRadioGroupAnswers.getLayoutParams());
                    answer.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            mNextQuestion.setEnabled(true);
                        }
                    });
                    mRadioGroupAnswers.addView(answer);
                }
            } else {
                if (mAnswerTitles != null && mAnswerTitles.size() > 0) {
                    for (int i = 0; i < mAnswerTitles.size(); i++) {
                        Answer answer1 = mAnswerTitles.get(i);
                        CheckBox answer = new CheckBox(getActivity());
                        answer.setText(answer1.getTitle().toString());
                        answer.setTextSize(24);
                        answer.setTextColor(Color.BLACK);
                        answer.setId(answer1.getId());
                        answer.setButtonDrawable(R.drawable.checkbox_selector);
                        answer.setLayoutParams(mRadioGroupAnswers.getLayoutParams());
                        answer.setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                // TODO Auto-generated method stub
                                mNextQuestion.setEnabled(true);
                            }
                        });
                        if (mAnswerHolder != null) {
                            mAnswerHolder.addView(answer);
                        }
                    }
                }

我将这些组/布局添加到:

<RadioGroup
            android:layout_marginTop="8dp"
            android:id="@+id/answerGroup"
            android:textColor="@android:color/black"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </RadioGroup>

        <LinearLayout
            android:id="@+id/answerTextHolder"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>

看起来文本实际上只是被图像隐藏 - 而不是放在图像上并重新调整图像的文本长度:

enter image description here

1 个答案:

答案 0 :(得分:1)

我明白了!诀窍是使用setBackgroundResource而不是setButtonDrawable:

answer.setBackgroundResource(R.drawable.radio_selector);

enter image description here

在做了一些测试后想到 - 如果您完全自定义,我建议也可以覆盖Radio / Checkbox按钮。