gpt4 book ai didi

android - 如何制作带有重叠文本的 RadioButton?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:14:18 30 4
gpt4 key购买 nike

我正在以编程方式将问题的多个答案添加为 RadioGroup 中的 RadioButton(请参见下面的代码和图片)。

我几乎实现了它,但我不想在旁边有一个带有文本的单选按钮(见下图),我只想有文本和背景。

我可以去掉单选按钮吗?

或者我可以将我当前的背景设置为单选按钮(具有选中/未选中状态),并添加一个与其重叠的文本吗?

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

while (c.isAfterLast() == false) {

RadioButton answer = new RadioButton(PCItem.this);
answer.setText(c.getString(c.getColumnIndex(DBAdapter.KEY_ANS_TEXT)));
answer.setBackgroundResource(R.drawable.btn_ans);
answer.setPadding((int)(30 * density), 0, 0, 0);
answer.setGravity(Gravity.CENTER_VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int)(775 * density), (int)(81 * density));
params.setMargins(0, (int)(20*density), 0, 0);
answer.setLayoutParams(params);

answer_container.addView(answer);

c.moveToNext();
}

enter image description here

最佳答案

您可以使用 radioButton.setButtonDrawable()。您还可以使用 StateListDrawable 为每个按钮状态(包括 checked/unchecked)指定不同的 drawable。 .

要完全删除按钮,您只需将其设置为null:

<RadioButton 
android:button="@null"
...
/>

请注意,通过代码 radioButton.setButtonDrawable(null); 不起作用!正确的方法是设置一个空的StateListDrawable:

radioButton.setButtonDrawable(new StateListDrawable());

如果你有更多的项目,你可以创建一个自定义样式来覆盖RadioButton的默认属性:

<style name="CustomRadioButton" parent="@android:style/Widget.CompoundButton.RadioButton">
<item name="android:button">@null</item>
<item name="android:paddingLeft">0dp</item>
</style>

关于android - 如何制作带有重叠文本的 RadioButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8910729/

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