如何定制动态编程布局-Android
我正在处理一些示例代码,我正在使用这些代码创建一个应用程序,该应用程序可以从服务器中提取调查。布局渲染是在java中完成的,而不是在XML中完成的,我遇到了一个问题,即问题和答案的标签和文本编辑框彼此挤在同一行上,而不是像XML渲染的默认情况那样,一行一行地放在另一行上 我想有上面的文本编辑框标签,使两者都可以在屏幕上伸展,并被看到更好 总体布局的代码如下所示:如何定制动态编程布局-Android,android,xml,android-layout,dynamic,Android,Xml,Android Layout,Dynamic,我正在处理一些示例代码,我正在使用这些代码创建一个应用程序,该应用程序可以从服务器中提取调查。布局渲染是在java中完成的,而不是在XML中完成的,我遇到了一个问题,即问题和答案的标签和文本编辑框彼此挤在同一行上,而不是像XML渲染的默认情况那样,一行一行地放在另一行上 我想有上面的文本编辑框标签,使两者都可以在屏幕上伸展,并被看到更好 总体布局的代码如下所示: private boolean DisplayForm() { try { ScrollVi
private boolean DisplayForm()
{
try
{
ScrollView sv = new ScrollView(this);
//need TableLayout Here!
final LinearLayout ll = new LinearLayout(this);
sv.addView(ll);
ll.setOrientation(android.widget.LinearLayout.VERTICAL);
ll.setBackgroundResource(R.color.background);
int i;
for (i=0;i<theForm.fields.size();i++) {
if (theForm.fields.elementAt(i).getType().equals("text")) {
theForm.fields.elementAt(i).obj = new EditSurvey(this,(theForm.fields.elementAt(i).isRequired() ? "*" : "") + theForm.fields.elementAt(i).getLabel(),"");
ll.addView((View) theForm.fields.elementAt(i).obj);
}
if (theForm.fields.elementAt(i).getType().equals("numeric")) {
theForm.fields.elementAt(i).obj = new EditSurvey(this,(theForm.fields.elementAt(i).isRequired() ? "*" : "") + theForm.fields.elementAt(i).getLabel(),"");
((EditSurvey)theForm.fields.elementAt(i).obj).makeNumeric();
ll.addView((View) theForm.fields.elementAt(i).obj);
}
if (theForm.fields.elementAt(i).getType().equals("choice")) {
theForm.fields.elementAt(i).obj = new SurveySpinners(this,(theForm.fields.elementAt(i).isRequired() ? "*" : "") + theForm.fields.elementAt(i).getLabel(),theForm.fields.elementAt(i).getOptions());
ll.addView((View) theForm.fields.elementAt(i).obj);
}
}
我省略了另一个代码片段,它使用微调器拉入标签,这几乎是一样的
这不是我的代码,只是为了澄清这是弗兰克·阿贝尔森的代码,但我正试图根据我的目的来解释它
任何帮助都会非常好。查看this.setOrientationandroid.widget.LinearLayout.VERTICAL;在你的EditSurvey课堂上,帮帮忙!是的,就是这样。非常感谢。
public class EditSurvey extends LinearLayout {
TextView label;
EditText txtBox;
public EditSurvey(Context context,String labelText,String initialText) {
super(context);
label = new TextView(context);
label.setText(labelText);
label.setTextColor(0xFF000000);
//label.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
txtBox = new EditText(context);
txtBox.setText(initialText);
//txtBox.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
this.addView(label);
this.addView(txtBox);
}