2014-12-04 89 views
0

我有一个布局,我想模仿一个列表视图通过编程添加项目一个在另一个之下。所以,我创建了这些项目是像这样的布局的.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); 
} 
+0

只是想知道,但为什么会使用ListView不够? – idunnololz 2014-12-04 22:30:53

+0

因为这是一个相当复杂的滚动视图布局 – erdomester 2014-12-04 22:43:22

+0

你能不能在for循环中使用'LayoutInflater.inflate()'所需的次数。初始化\设置每个布局并将其添加到所需的视图? – idunnololz 2014-12-04 23:30:11

回答

0

不知道你为什么会想这样做,但你可以只使用一个LayoutInflater来将该视图的x扩大并将它们添加到指定的ViewGroup。例如:

LayoutInflater inflater = LayoutInflater.from(context); 
for (int i = 0; i < numberOfViews; i++) { 
    View v = inflater.inflate(R.layout.view_to_add, parentView, false); 
    setup(v); 
    parentView.addView(v); 
} 

哪里setup()是你定义一个函数,它设置工作,如设置的TextView

例如文本,在你的设置功能可能是这个样子:

private void setup(View v) { 
    TextView tv = (TextView) v.findViewById(R.id.name); 
    // set the text of the text view 
    tv.setText("Hello!"); 

    // set the click listener for the view... 
    v.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // do stuff here... 
     } 
    }); 
} 
+0

我仍然不知道如何到我想要的。什么是parentView?如何在视图上设置onclicklistener? – erdomester 2014-12-05 07:29:13

+0

'parentView'是您希望添加视图到的'ViewGroup'。这通常是一个'LinearLayout'。我已经更新了答案,以显示如何设置视图的“OnClickListener”的示例。 – idunnololz 2014-12-05 19:44:40

0

如果您发布的XML文件bucketprofile_tips.xml则需要从

这里改变

TextView tv_username = (TextView) view.findViewById(R.id.tv_username); 

由于bucketprofile_tips.xmltv_title ID找不到你TextView。所以你得到Null Pointer Exception

+0

这只是一个错误,在xml中有一个tv_username,这个答案不是我的问题的解决方案 – erdomester 2014-12-05 08:14:07

+0

尝试从LayoutInflater中更改此inflater = LayoutInflater.from(BucketProfileActivity.this);到LayoutInflater inflater = getLayoutInflater();而且我已经在我的第一行中指定了此解决方案。 – Piyush 2014-12-05 08:33:49

+0

感谢您的帮助,但事实并非如此。请参阅我更新的帖子。 – erdomester 2014-12-05 08:38:57