gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:59:34 25 4
gpt4 key购买 nike

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

我能够创建自定义按钮 View ,但由于某些原因文本不显示。知道我哪里出错了吗?

复选框选择器:

<?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

最佳答案

我想通了!诀窍是使用 setBackgroundResource 而不是 setButtonDrawable:

answer.setBackgroundResource(R.drawable.radio_selector);

enter image description here

经过一些测试后的想法 - 如果您完全自定义,我建议也覆盖可绘制的单选/复选框按钮。

关于Android 自定义复选框/单选按钮文本不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22752999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com