如何自定义动态、程序化布局 - Android
我正在处理一些示例代码,用于创建一个从服务器提取调查的应用程序。布局渲染是在java中而不是在XML中完成的,我遇到了一个问题,即标签和文本编辑框(分别用于问题和答案)在同一行上相互挤压,而不是一个单独地放在另一个之上行是默认的 XML 渲染。
我希望将标签放在文本编辑框上方,以便两者都可以在屏幕上延伸并更好地看到。
整体布局的代码在这里:
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);
}
}
用于拉入标签和文本编辑框的代码在这里:
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);
}
我省略了使用微调器拉入标签的其他代码片段,这几乎是相同的。
这不是我的代码,只是为了清楚起见(这是 Frank Abelson 的),但我试图将其调整为我的目的。
任何帮助都会很棒。
I am dealing with some sample code I am using to create an app that pulls in Surveys from a server. The layout rendering is done in the java rather than in XML and I am having a problem having labels and text editing boxes (for questions and answers, respectively) squishing into eachother on the same line rather than sitting one on top of the other on seperate lines as is the default with rendering in XML.
I want to have the Labels above the text edit boxes so both can stretch across the screen and be seen better.
the code for the overall layout it here:
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);
}
}
the code for pulling in the labels and text editing boxes is here:
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);
}
I've left out the other code snippet that pulls in the labels with spinners, which is pretty much the same.
This is not my code, just to be clear (it's Frank Abelson's), but I am trying to wrangle it to my purposes.
any help would be fantastic.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论