动态添加textview android的问题

时间:2013-07-29 04:46:32

标签: android expandablelistview textview

我有一个可扩展的列表视图,其中包含一个提交按钮。单击提交按钮时,应在前一个文本视图之前创建一个文本视图。我尝试使用以下代码..但它没有正确显示。正确添加了两个textview,但之后它替换了旧的textviews ..请告诉我我的代码有什么问题

   imGbtn.setOnClickListener(new OnClickListener() {                
            public void onClick(View v) {
    int prevTextViewId = 0;
    int curTextViewId = prevTextViewId + 1;
    Random rnd = new Random();
    final TextView textView = new TextView(mContext);
    textView.setText(data);     
    textView.setId(curTextViewId);
    final RelativeLayout.LayoutParams params = 
    new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,       RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.BELOW, textView.getId());
    textView.setLayoutParams(params);
    tvll.addView(textView, params);
    }
    });

3 个答案:

答案 0 :(得分:1)

看起来你正试图将视图置于其下方:

params.addRule(RelativeLayout.BELOW, textView.getId());

我认为你希望它是这样的:(假设你希望它高于你的问题,就像你在问题中所说的那样)

params.addRule(RelativeLayout.ABOVE, prevTextViewId);

您还需要在此方法之外跟踪prevTextViewId。让它成为你班级的一部分。

int prevTextViewid = 0; //part of your class
   ...

   imGbtn.setOnClickListener(new OnClickListener() {                
       public void onClick(View v) {
       int curTextViewId = prevTextViewId + 1;
       Random rnd = new Random();
       TextView textView = new TextView(mContext);
       textView.setText(data);     
       textView.setId(curTextViewId);
       RelativeLayout.LayoutParams params = 
          new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
       params.addRule(RelativeLayout.BELOW, prevTextViewId );
       textView.setLayoutParams(params);
       tvll.addView(textView, params);
       prevTextViewId = curTextViewId;
   }
});

答案 1 :(得分:0)

通过查看你的tvll,你似乎已经采用了textview的线性布局,所以 试试这个 -

imGbtn.setOnClickListener(new OnClickListener() {                
        public void onClick(View v) {
  int prevTextViewId = 0;
  int curTextViewId = prevTextViewId + 1;
  Random rnd = new Random();
  final TextView textView = new TextView(mContext);
  textView.setText(data);     
  textView.setId(curTextViewId);
  final LinearLayout.LayoutParams params = 
  new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);

  textView.setLayoutParams(params);
  tvll.addView(textView, params);
}
});

答案 2 :(得分:0)

    protected void addTextView() {
    int prevId = 1;
    for (int m = 1; m <= 3; m++) {
        TextView textView = new TextView(this);
        textView.setText("TextView  : " + m);
        textView.setId(m);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.BELOW, prevId);
        textView.setLayoutParams(params);
        relative.addView(textView);
        prevId = m;
    }
}

将它们添加到循环中......我们可以用你自己的方式实现......这只是使用相对布局......