我有一个 RelativeLayout,我想插入一些 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>
以及我如何以编程方式执行此操作:
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 处于相同的位置。