0
制作一个简单的程序将生成一个多选表单。我有一个sing_select.xml,作为制作每个问题的模板。然后,在代码中,我想用自定义的一堆模板填充我的main.xml。虽然它对于第一个问题很有用,但以后的任何问题都不会显示出来。不知道我做错了什么。我知道没有重叠,因为我手动隐藏了第一个问题。Android Dev:无法从XML模板动态生成多个布局
Java文件
public class FormFillerActivity extends Activity
{
private LinearLayout mQuestionList;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Must come before setContentView or program crashes
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Must set before accessing layout elements or program crashes
setContentView(R.layout.main);
mQuestionList = (LinearLayout) findViewById(R.id.Body_Layout);
initForm();
}
private void initForm()
{
int count = 1;
ArrayList<String> answers = new ArrayList<String>();
answers.add("Single");
answers.add("Married");
answers.add("Separated");
answers.add("Divorced");
mQuestionList.addView(addSingSelectQuestion(count++, "What is your marital status?", answers));
answers.clear();
answers.add("Male");
answers.add("Female");
mQuestionList.addView(addSingSelectQuestion(count++, "What is your gender?", answers));
}
private View addSingSelectQuestion(int count, String question, ArrayList<String> answers)
{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View container = inflater.inflate(R.layout.sing_select, null);
((TextView) container.findViewById(R.id.Sing_Select_Num)).setText(count + ") ");
((TextView) container.findViewById(R.id.Sing_Select_Text)).setText(question);
RadioGroup rg = (RadioGroup) container.findViewById(R.id.Sing_Select_Answer);
//Generate radio group answers
Iterator<String> it = answers.iterator();
while (it.hasNext())
{
RadioButton rb = new RadioButton(rg.getContext());
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
String ans = it.next();
rb.setId(answers.indexOf(ans));
rb.setLayoutParams(params);
rb.setText(ans);
rb.setTextColor(getResources().getColor(R.color.black));
rb.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.txt_normal));
rg.addView(rb);
}
return container;
}
}
main.xml中(去掉了不相关的UI元素)
<?xml version="1.0" encoding="utf-8"?>
...
<ScrollView
android:id="@+id/Body_Scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/Footer"
android:layout_below="@id/Title"
android:scrollbars="vertical" >
<LinearLayout
android:id="@+id/Body_Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/marg_normal"
android:padding="@dimen/pad_large" >
</LinearLayout>
</ScrollView>
...
sing_select.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Sing_Select_Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:padding="8dp" >
<TextView
android:id="@+id/Sing_Select_Num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#) "
android:textColor="@color/black"
android:textSize="@dimen/txt_normal" />
<TextView
android:id="@+id/Sing_Select_Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/Sing_Select_Num"
android:layout_toRightOf="@+id/Sing_Select_Num"
android:text="The Question?"
android:textColor="@color/black"
android:textSize="@dimen/txt_normal" />
<RadioGroup
android:id="@+id/Sing_Select_Answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Sing_Select_Text"
android:layout_below="@+id/Sing_Select_Text"
android:layout_toLeftOf="@+id/Sing_Select_Trans_Button" >
</RadioGroup>
<Button
android:id="@+id/Sing_Select_Trans_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/btn_big"
android:padding="8dp"
android:text="Accept"
android:textSize="@dimen/txt_button" />
</RelativeLayout>
男人我是个白痴。这解决了它。当我想念那样的事情时,恨它。 – 2012-03-01 22:11:24
@JaySoyer如果我的回答解决了您的问题,请将其标记为正确。 – Luksprog 2012-03-02 10:50:00
对不起人。新的张贴在这里。不知道我能做到这一点。现在应该打上标记。 – 2012-03-02 13:20:56