如何使RadioButton不以编程方式在自定义适配器中膨胀?
全部,如何使RadioButton不以编程方式在自定义适配器中膨胀?
我有一个自定义适配器,用于将测验问题/答案充入ListView。每个问题(ListView中的单个项目)具有可变数量的答案,(但是最大数量的答案(5)是静态的)。
除了使用答案字符串填充每个RadioButton的文本(使用setText)之外,我还想使用自定义适配器来评估RadioGroup中的所有RadioButton是否都有与之相关的答案并相应地(或不) 。理想情况下,我想以编程方式“放弃”RadioButton或等效的效果,因为我能够评估自定义适配器中的答案有效性。我试过radioButton.setHeight(0)
和radioButton.setMaxHeight(0)
没有运气。
XML资源文件我拉answerStrings从被定义为这样:在一个字符串数组,每题5个答案索引目的的假设
答案XML资源文件存储所有的答案字符串。 有效答案由字符串数组中的非空项表示。没有有效的答案是由一个空的项目存根表示的。
<string-array
name="answer_array">
<!-- question 1 answers -->
<item>question 1 answer A</item>
<item>question 1 answer B</item>
<item>question 1 answer C</item>
<item>question 1 answer D</item>
<item>question 1 answer E</item>
<!-- question 2 answers -->
<item>question 2 answer A</item>
<item>question 2 answer B</item>
<item></item>
<item></item>
<item></item>
<!-- question 3 answers -->
<item>question 3 answer A</item>
<item>question 3 answer B</item>
<item>question 3 answer C</item>
<item></item>
<item></item>
</string-array>
我测验布局文件如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical" >
<!-- quiz question -->
<TextView
android:id="@+id/question"
android:layout_gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"/>
<!-- answers for each question -->
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="5dip"
android:id="@+id/radiogroup">
<RadioButton
android:id="@+id/A_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doNothing"/>
<RadioButton
android:id="@+id/B_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doNothing"/>
<RadioButton
android:id="@+id/C_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doNothing"/>
<RadioButton
android:id="@+id/D_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doNothing"/>
<RadioButton
android:id="@+id/E_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doNothing"/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
在我的自定义适配器,我尝试做以下;
private ViewHolder initRadioButtonText(ViewHolder holder, Custom quiz_row_data) {
/* check if valid answer */
int length = (quiz_row_data.getA_AnswerString()).length();
if(length == 0) {
holder.A_button.setHeight(0);
//holder.A_button.setMaxHeight(0);
}
else
holder.A_button.setText(quiz_row_data.getA_AnswerString());
/* ..... etc etc for the other answer buttons ..... */
return holder;
}
如果A_button
是你不想显示该按钮就用
holder.A_button.setVisibility(RadioButton.GONE);
,直到调用
holder.A_button.setVisibility(RadioButton.VISIBLE);
如果这将彻底从layout
删除不担心将其移除以腾出空间供其他View
s使用
holder.A_button.setVisibility(RadioButton.INVISIBLE);
如果你真的想按钮改变高度为零,正确的方法是使用LayoutParams
:
holder.A_button.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, 0));
否则,第二解决方案是切换能见度为codeMagic显示。
感谢您的答案! – 2013-03-26 18:27:38
这非常适合以编程方式从布局中删除特定按钮。谢谢codeMagic。我也应该注意其他用户看这个答案;每个按钮的可见性需要明确设置,尽管独立使用的问题,即:itemholder.D_button.setVisibility(RadioButton.VISIBLE)除了设置RadioButton文本 – 2013-03-26 18:29:01
很高兴我可以帮助!这就是为什么我的第二块在那里,但我想我可以更好地解释它......好的音符! :) – codeMagic 2013-03-26 18:30:22