由网友(再贱"就再见)分享简介:我有一个RelativeLayout的,我想插入一些textViews。I have an RelativeLayout and I would like to insert some textViews.下面是code:我有一个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);
相关推荐
最新文章