【问题标题】:How can I add textview dynamically from arraylist?如何从arraylist动态添加textview?
【发布时间】:2017-09-13 01:33:00
【问题描述】:

是否可以在相对布局中动态添加 textView,从数组列表中获取信息?我会解释的。例如,我有一个从数据库中获得的带有五个单词的 arralist。然后我会在其布局中创建 5 个不同的 textView。 我不能使用列表视图,因为它是用户选择这些 TextView 的位置。

这是我正在工作的代码,因此我能够创建更多 TextView,但不能从数组列表中创建。我还需要给每个TextView的上边距赋予不同的值!

RelativeLayout rl = (RelativeLayout) findViewById(R.id.sundayRelativeLayout);
    TextView iv;
    RelativeLayout.LayoutParams params;

    

    float d = this.getResources().getDisplayMetrics().density;

    iv = new TextView(this);
    iv.setBackgroundColor(Color.YELLOW);
    int marginTopDip = 180; // margin in dips
    int marginHeightDips = 60; // margin in dips

    int height = (int)(marginHeightDips * d);
    int margintop = (int)(marginTopDip * d);
    params = new RelativeLayout.LayoutParams(500, height);
    params.topMargin = margintop;
    rl.addView(iv, params);



    iv = new TextView(this);
    iv.setBackgroundColor(Color.RED);
    params = new RelativeLayout.LayoutParams(30, 40);
    params.topMargin = 900;
    rl.addView(iv, params);

【问题讨论】:

  • 您可以通过编程方式创建 TextView 并使用LayoutParams定位它们

标签: android textview


【解决方案1】:

像这样创建

ArrayList<Integer> list=new ArrayList<Integer>();

如果您的列表大小为 5

for(int i=0;i<list.size();i++){
TextView tv=new TextView(this);
tv.setText(""+list.get(i));
}

【讨论】:

  • 谢谢!这是工作!但是如何让我的 TextView 彼此处于不同的边距顶部?
  • LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.setMargins(左,上,右,下); yourTextView.setLayoutParams(params);
  • 这是正确的,但在这种情况下我还需要从数组列表中接收信息,以便它们与 texview 相关联。
  • for(int i=0;i
【解决方案2】:
final int N = 10; // total number of textviews to add

final TextView[] myTextViews = new TextView[N]; // create an empty array;

for (int i = 0; i < N; i++) {
    // create a new textview
    final TextView rowTextView = new TextView(this);

    // set some properties of rowTextView or something
    rowTextView.setText("This is row #" + i);

    // add the textview to the linearlayout
    myLinearLayout.addView(rowTextView);

    // save a reference to the textview for later
    myTextViews[i] = rowTextView;
}

【讨论】:

    【解决方案3】:

    是的,您可以像这样将 textview 添加到相对布局中。

    RelativeLayout container = (RelativeLayout) findViewById(R.id.container);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT);
    
        ArrayList<Integer> list=new ArrayList<Integer>();
    
        for(int i=0;i<list.size();i++){
           TextView textView = new TextView(this);
           textView1.setText("A"+i);
           textView1.setId(i);
           container.addView(textView);
           params.addRule(RelativeLayout.BELOW,i);      
        }   
    

    【讨论】:

      猜你喜欢
      • 2013-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多