我有一个布局,我想模仿一个列表视图通过编程添加项目一个在另一个之下。所以,我创建了这些项目是像这样的布局的.xml:膨胀布局和使用其子项
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_lista"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_marginTop="5dp"
android:layout_toLeftOf="@+id/rellay_btn"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:text="Some name"
android:textColor="#3F3F3F"
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/iv_follow"
android:text="Some text"
android:textColor="#3F3F3F"
android:textSize="13sp" />
</LinearLayout>
在活动中,我想生成此布局的N,填充文字textviews,也是我想设置一个onClickListener对他们。 n是数组的大小。请帮助
注意:一旦活动加载,布局的数量将不会改变,也无法移除布局。
这是我现在有,但布局显示在活动的顶部,而不是下面一个TextView:
LayoutInflater inflater = LayoutInflater.from(BucketProfileActivity.this);
for (int i = 0; i < 3; i++) {
View view = inflater.inflate(R.layout.bucketprofile_tips, null);
view.setId(i);
TextView tv_username = (TextView) view.findViewById(R.id.tv_username);
tv_username.setText(String.valueOf(i));
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rl.addRule(RelativeLayout.BELOW, R.id.tv_nemkell);
addContentView(view, rl);
}
只是想知道,但为什么会使用ListView不够? – idunnololz 2014-12-04 22:30:53
因为这是一个相当复杂的滚动视图布局 – erdomester 2014-12-04 22:43:22
你能不能在for循环中使用'LayoutInflater.inflate()'所需的次数。初始化\设置每个布局并将其添加到所需的视图? – idunnololz 2014-12-04 23:30:11