1

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

   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);
    }
    });
4

3 回答 3

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;
   }
});
于 2013-07-29T04:57:51.347 回答
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);
}
});
于 2013-07-29T05:00:04.993 回答
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;
    }
}

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

于 2013-07-29T05:32:50.540 回答