我以编程方式使用相对布局。它有多个textview但它显示水平而不是垂直。如何以编程方式从段落视图中获取它们?
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
int x=10;
int y=30;
for(int i=0;i<arrayList.size();i++)
{
String str=arrayList.get(i);
int strLength=arrayList.size();
if(arrayList.get(i).equals("Name")){
y=y+10;
x=65;
}
else
{
x=x+strLength+60;
}
//tv=(TextView) findViewById(R.id.tv1);
tv=new TextView(this);
tv.setText(arrayList.get(i).toString());
tv.setPadding(x, y, 0, 0);
layout.addView(tv, params);
}
scroll.addView(layout);
this.setContentView(scroll);
答案 0 :(得分:2)
RelativeLayout
没有名为Orientation
的媒体资源。您必须为每个textviews
提供ID并添加如下规则。
tv.addRule(RelativeLayout.BELOW, someOtherView.getId());
所以在你的情况下,我认为它会是这样的:
for(int i=0;i<arrayList.size();i++)
{
String str=arrayList.get(i);
int strLength=arrayList.size();
if(arrayList.get(i).equals("Name")){
y=y+10;
x=65;
}
else
{
x=x+strLength+60;
}
//tv=(TextView) findViewById(R.id.tv1);
tv=new TextView(this);
tv.setId(i);
tv.setText(arrayList.get(i).toString());
tv.setPadding(x, y, 0, 0);
layout.addView(tv, params);
if (i>0)
tv.addRule(RelativeLayout.BELOW, i-1);
}
希望这有帮助。
答案 1 :(得分:0)
您只需要向params RelativeLayout.BELOW
试试这个......
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
int x=10;
int y=30;
for(int i=0;i<arrayList.size();i++)
{
String str=arrayList.get(i);
int strLength=arrayList.size();
if(arrayList.get(i).equals("Name")){
y=y+10;
x=65;
}
else
{
x=x+strLength+60;
}
//tv=(TextView) findViewById(R.id.tv1);
tv=new TextView(this);
tv.setText(arrayList.get(i).toString());
tv.setPadding(x, y, 0, 0);
if(layout.getChildCount() != 0)
params.addRule(RelativeLayout.BELOW, (layout.getChildAt(layout.getChildCount() - 1)).getId());
layout.addView(tv, params);
}
scroll.addView(layout);
this.setContentView(scroll);
希望这会有所帮助......