Android:以编程方式在RelativeLayout中添加textViews [英] Android: Add textViews in RelativeLayout Programmatically
本文介绍了Android:以编程方式在RelativeLayout中添加textViews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个RelativeLayout,我想插入一些textViews.
I have an RelativeLayout and I would like to insert some textViews.
这是代码:
<RelativeLayout
android:id="@+id/JournalSearchListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="30dp"
android:layout_below="@+id/JournalsSearchTextView"
android:orientation="vertical"
android:cacheColorHint="#00000000">
<ProgressBar
android:id="@+id/JournalSearchProgressBar"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
以及我如何以编程方式执行此操作:
And how I am doing this programmatically:
RelativeLayout journals = (RelativeLayout) findViewById(R.id.JournalSearchListView);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for (int i=0;i< authorNames.size();i++) {
TextView tv = new TextView(this);
tv.setId(i);
tv.setText(authorNames.get(i));
tv.setTextColor(Color.BLACK);
Integer a = tv.hashCode();
map.put(a,authorNames.get(i));
params1.addRule(RelativeLayout.BELOW, tv.getId());
journals.addView(tv, params1);
tv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
System.out.println("Clicked "+ map.get(v.hashCode()) );
}
});
}
但是问题是每个textView与其他textView处于同一位置.
But the problem is that each textView is in the same position as the others.
推荐答案
params1.addRule(RelativeLayout.BELOW, tv.getId());
您将其设置为低于自身?
如果您想让他们在彼此的下方,则可以这样做:
You are setting it to be below itself?
If you want them below eachother do it this way:
RelativeLayout journals = (RelativeLayout);
findViewById(R.id.JournalSearchListView);
LinearLayout lL = new LinearLayout(context);
lL.setOrientation(LinearLayout.VERTICAL);
for (int i=0;i< authorNames.size();i++) {
TextView tv = new TextView(this);
tv.setId(i);
tv.setText(authorNames.get(i));
tv.setTextColor(Color.BLACK);
Integer a = tv.hashCode();
map.put(a,authorNames.get(i));
if(i!=0){
params1.addRule(RelativeLayout.BELOW, i-1);
}
tv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
System.out.println("Clicked "+ map.get(v.hashCode()) );
}
});
lL.addView(tv);
}
journals.addview(lL);
这篇关于Android:以编程方式在RelativeLayout中添加textViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文