Java Android-Dev:Can';t从XML模板动态生成多个布局
制作一个简单的程序,生成一个选择题表格。我有一个sing_select.xml,它作为制作每个问题的模板。然后,在代码中,我想用一堆定制的模板填充main.xml。尽管它对第一个问题非常有效,但随后的任何问题都不会显示出来。不知道我做错了什么。我知道没有重叠,因为我手动隐藏了第一个问题 Java文件Java Android-Dev:Can';t从XML模板动态生成多个布局,java,android,xml,android-layout,dynamic,Java,Android,Xml,Android Layout,Dynamic,制作一个简单的程序,生成一个选择题表格。我有一个sing_select.xml,它作为制作每个问题的模板。然后,在代码中,我想用一堆定制的模板填充main.xml。尽管它对第一个问题非常有效,但随后的任何问题都不会显示出来。不知道我做错了什么。我知道没有重叠,因为我手动隐藏了第一个问题 Java文件 公共类FormFillerActivity扩展活动 { 私人线路布局mQuestionList; /**在首次创建活动时调用*/ @凌驾 创建时的公共void(Bundle savedInstan
公共类FormFillerActivity扩展活动
{
私人线路布局mQuestionList;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//必须在setContentView或程序崩溃之前到达
this.requestWindowFeature(Window.FEATURE\u NO\u TITLE);
//在访问布局元素或程序崩溃之前必须设置
setContentView(R.layout.main);
mQuestionList=(LinearLayout)findViewById(R.id.Body_布局);
initForm();
}
私有void initForm()
{
整数计数=1;
ArrayList answers=新的ArrayList();
答案。添加(“单一”);
答案。加上(“已婚”);
答案。添加(“分开”);
答复。添加(“离婚”);
addView(addSingSelectQuestion(count++,“您的婚姻状况如何?”,答案));
答案:clear();
答案。加上(“男性”);
答案。加上(“女性”);
addView(addSingSelectQuestion(count++,“你的性别是什么?”,答案));
}
私有视图添加选择问题(整数计数、字符串问题、数组列表答案)
{
LayoutFlater充气器=(LayoutFlater)getSystemService(Context.LAYOUT\u充气器\u服务);
查看容器=充气机。充气(R.layout.sing\u select,null);
((TextView)container.findviewbyd(R.id.Sing_Select_Num)).setText(count+”);
((TextView)container.findviewbyd(R.id.Sing_Select_Text)).setText(问题);
RadioGroup rg=(RadioGroup)container.findViewById(R.id.Sing\u Select\u response);
//生成无线电组答案
Iterator it=answers.Iterator();
while(it.hasNext())
{
RadioButton rb=新的RadioButton(rg.getContext());
RadioGroup.LayoutParams params=新的RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_内容,
RadioGroup.LayoutParams.WRAP_内容);
字符串ans=it.next();
rb.setId(answers.indexOf(ans));
rb.setLayoutParams(参数);
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);
}
返回容器;
}
}
main.xml(去掉不相关的UI元素)
...
...
sing_select.xml
尝试将*Body\u布局*线性布局的方向设置为垂直
。我想你会在第一行之后的下一行(充气视图的宽度设置为填充父视图)将其推出屏幕。伙计,我真是个白痴。这就解决了问题。讨厌我错过这样的事情。@JaySoyer如果我的答案解决了你的问题,请把它标记为正确。对不起,伙计。新到这里张贴。我不知道我能做到。现在应该标记。
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;
}
}
<?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>
...
<?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>