我正在尝试使用来自 Http 请求的信息以编程方式创建自定义单选按钮/复选框(在应用程序内创建调查)。我已经尝试了Android Checkbox text not displayed但文本仍然没有出现中建议的解决方案。
我能够创建自定义按钮视图,但由于某种原因文本不显示。知道我哪里出错了吗?
复选框选择器:
<?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>
无线电选择器:
<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>
看起来文本实际上只是被图像隐藏了 - 而不是被放在图像上并根据文本长度重新调整图像大小: