如何使用自定义宽度动态添加 RadioButton?
How to Add a RadioButton dynamically with custom Width?
我想在 Android 应用程序中动态添加一组单选按钮。我在 Xml 文件中添加了一个 RadioGroup:
<RadioGroup
android:id="@+id/sugg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Question"
android:layout_alignRight="@+id/number"
android:layout_below="@+id/Question"
android:layout_marginTop="18dp" >
</RadioGroup>
我有一个 JsonArray,其中包含应显示为单选按钮的响应列表,以便用户可以 select 只有一个选择。我能够显示单选按钮但没有文本。
它是这样显示的:
我的问题是如何显示文本?
这是我开发的功能:
public void addRadioButtons(RadioGroup rad,JSONObject jObj){
try {
JSONArray answ=(JSONArray) jObj.get("Responses");
for(int i=0;i<answ.length();i++){
JSONObject an=(JSONObject) answ.get(i);
RadioButton radioButton = new RadioButton(c);
radioButton.setLayoutParams (new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
radioButton.setId(i);
radioButton.setText(an.getString("DescriptionEn"));
radioButton.setWidth(200);
rad.addView(radioButton);
}
} catch (Exception e) {
e.printStackTrace();
}
}
我检查了单选按钮的文本值,它包含我想要显示的文本,但宽度始终为 0,我无法更改它。
我添加这条指令 radioButton.setWidth(200);
对于我使用的 LayoutParams import android.widget.RadioGroup.LayoutParams;
谢谢。
最终工作代码(以 JSON 对象和 RadioGroup 作为参数):
public void addRadioButtons(RadioGroup rad,JSONObject jObj){
try {
JSONArray answ=(JSONArray) jObj.get("Responses");
for(int i=0;i<answ.length();i++){
JSONObject an=(JSONObject) answ.get(i);
RadioButton radioButton = new RadioButton(c);
radioButton.setLayoutParams (new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
radioButton.setId(i);
radioButton.setText(an.getString("DescriptionEn"));
radioButton.setTextColor(Color.GREY)
rad.addView(radioButton);
}
} catch (Exception e) {
e.printStackTrace();
}
}
我想在 Android 应用程序中动态添加一组单选按钮。我在 Xml 文件中添加了一个 RadioGroup:
<RadioGroup
android:id="@+id/sugg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Question"
android:layout_alignRight="@+id/number"
android:layout_below="@+id/Question"
android:layout_marginTop="18dp" >
</RadioGroup>
我有一个 JsonArray,其中包含应显示为单选按钮的响应列表,以便用户可以 select 只有一个选择。我能够显示单选按钮但没有文本。
它是这样显示的:
我的问题是如何显示文本?
这是我开发的功能:
public void addRadioButtons(RadioGroup rad,JSONObject jObj){
try {
JSONArray answ=(JSONArray) jObj.get("Responses");
for(int i=0;i<answ.length();i++){
JSONObject an=(JSONObject) answ.get(i);
RadioButton radioButton = new RadioButton(c);
radioButton.setLayoutParams (new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
radioButton.setId(i);
radioButton.setText(an.getString("DescriptionEn"));
radioButton.setWidth(200);
rad.addView(radioButton);
}
} catch (Exception e) {
e.printStackTrace();
}
}
我检查了单选按钮的文本值,它包含我想要显示的文本,但宽度始终为 0,我无法更改它。
我添加这条指令 radioButton.setWidth(200);
对于我使用的 LayoutParams import android.widget.RadioGroup.LayoutParams;
谢谢。
最终工作代码(以 JSON 对象和 RadioGroup 作为参数):
public void addRadioButtons(RadioGroup rad,JSONObject jObj){
try {
JSONArray answ=(JSONArray) jObj.get("Responses");
for(int i=0;i<answ.length();i++){
JSONObject an=(JSONObject) answ.get(i);
RadioButton radioButton = new RadioButton(c);
radioButton.setLayoutParams (new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
radioButton.setId(i);
radioButton.setText(an.getString("DescriptionEn"));
radioButton.setTextColor(Color.GREY)
rad.addView(radioButton);
}
} catch (Exception e) {
e.printStackTrace();
}
}