如何在android中包装动态创建的textviews?

时间:2016-06-07 10:39:16

标签: java android android-layout

我在线性布局(lay)中创建了各种文本视图。我希望当textview伸出屏幕时,textview会自动跳到下一行,而不是跳出屏幕。我是android的初学者,而不是如何实现它。

这是我的代码:

    for (int i = 0; i < string.size(); i++) {
         final TextView txt = new TextView(con);
         txt.setText(string.get(i));
         lay.addView(txt);   
        }

3 个答案:

答案 0 :(得分:0)

试试这个

TextView txt = new TextView(context);
txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

答案 1 :(得分:0)

private void populateViews(LinearLayout linearLayout, ArrayList<TextView> views, View extraView)
{
    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    float mWidth = displaymetrics.widthPixels;
    extraView.measure(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    linearLayout.removeAllViews();
    int maxWidth = (int) mWidth - extraView.getMeasuredWidth() - 10;

    linearLayout.setOrientation(LinearLayout.VERTICAL);

    LinearLayout.LayoutParams params;
    LinearLayout newLL = new LinearLayout(mContext);
    newLL.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    newLL.setGravity(Gravity.LEFT);
    newLL.setOrientation(LinearLayout.HORIZONTAL);

    int widthSoFar = 0;

    for (int i = 0; i < views.size(); i++)
    {
        LinearLayout LL = new LinearLayout(mContext);
        LL.setOrientation(LinearLayout.HORIZONTAL);
        LL.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
        LL.setLayoutParams(new ListView.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        views.get(i).measure(0, 0);
        params = new LinearLayout.LayoutParams(views.get(i).getMeasuredWidth(), LinearLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(10, 2, 10, 2);

        LL.addView(views.get(i), params);
        LL.measure(0, 0);
        widthSoFar += views.get(i).getMeasuredWidth();
        if (widthSoFar >= maxWidth)
        {
            linearLayout.addView(newLL);

            newLL = new LinearLayout(mContext);
            newLL.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            newLL.setOrientation(LinearLayout.HORIZONTAL);
            newLL.setGravity(Gravity.LEFT);
            params = new LinearLayout.LayoutParams(LL.getMeasuredWidth(), LL.getMeasuredHeight());
            newLL.addView(LL, params);
            widthSoFar = LL.getMeasuredWidth();
        }
        else
        {
            newLL.addView(LL);
        }
    }
    linearLayout.addView(newLL);
}

这种方法应该可以帮到你。我已经把这个方法做了很久,所以通过一些修改你应该可以使用它。第一个参数是LinearLayout,它将是基本垂直线性布局,它将充当视图的主要容器。第二个参数是您必须添加的实际视图。在我的情况下,第三个参数是我正在使用的图像像占位符。

答案 2 :(得分:0)

您也可以这样添加。

object A,

object B,

temp c = B,

B = A,

A = c,

<强> Layout_Design:

       for (int i = 0; i < string.size(); i++) {
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = (View) inflater.inflate(
                    R.layout.Layout_Design, null);
            TextView txt = (TextView) view.findViewById(R.id.some_text);
            txt.setText("for runtime change");
            layout.addView(view);
            // layout is linear layout in my case.
       }