安卓:添加textViews在RelativeLayout的编程方式 [英] Android: Add textViews in RelativeLayout Programatically
本文介绍了安卓:添加textViews在RelativeLayout的编程方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个RelativeLayout的,我想插入一些textViews。
I have an RelativeLayout and I would like to insert some textViews.
下面是code:
<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 programatically:
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的是在相同的位置等。
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);
这篇关于安卓:添加textViews在RelativeLayout的编程方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文