Android以编程方式将多个Textview添加到布局换行符

时间:2016-12-08 14:59:54

标签: android layout textview line-breaks

我需要以编程方式将TextViews添加到LayoutRelativeLayoutLinearLayout我不在乎)。

我想要这样的事情:

enter image description here

textview 4之后,右侧没有更多空间,因此下一个TextView将被放置在新的Line下方。

这就是我所做的:

LinearLayout文件中创建xml

<LinearLayout
    android:id="@+id/my_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
</LinearLayout>

在我的代码中,我循环遍历Objects列表并为每个objekt添加新的TextView

LinearLayout myLinearLayout = (LinearLayout) itemView.findViewById(R.id.my_linear_layout);

for(MyObject object : myObjectList) {
          final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
          params.setMargins(20, 0, 20, 0); // llp.setMargins(left, top, right, bottom);

          final TextView textView = new TextView(context);
          textView.setText(object.getName());
          textView.setLayoutParams(params);

          myLinearLayout.addView(textView, params);
}

但这是我得到的结果:

enter image description here

我原本想过垂直添加LinearLayouts而不是TextViews,但我的问题仍然是我不知道何时需要添加下一个下一个,我的意思是,当有在右侧不再有空间。

我还尝试使用TableLayout,但问题仍然存在,我不知道何时需要添加下一个row

有什么想法吗?也许有一个视图已经解决了这个问题,我不知道......

0 个答案:

没有答案