动态地将视图添加到彼此下方和下方的水平线性布局

时间:2018-04-19 10:50:32

标签: android android-linearlayout

我有一个recyclerview,我必须在其中显示标题和说明。 descrtiption由n个项目(TextViews)组成,这些项目将彼此相邻地动态添加,如图像所示。但是,如果项目宽度大于屏幕宽度,则应转到下一行。

目前我有一个水平线性布局,如果没有足够的空间,我想在新线上添加新项目(TextView)。我的实现给出了下面的图像错误。我希望输出像图像2

我该如何实施?有没有更好的方法来做到这一点。

有没有办法检查TextViews的屏幕宽度而不向hirarchy添加视图?

Actual output

Expected output

1 个答案:

答案 0 :(得分:0)

1)在xml布局中创建一个linearlayout,如下所示:

<LinearLayout
                android:id="@+id/llKeyword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="25dp"
                android:layout_marginRight="10dp"
                android:layout_marginStart="15dp"
                android:layout_marginTop="10dp"
                android:orientation="vertical">

            </LinearLayout>

2)现在创建一个xml布局 view_textview.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tvName"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:padding="8dp"
    android:text="sadfjkhsdf"
    android:singleLine="true"
    android:textColor="@android:color/black" />

3)现在创建如下方法:

void setKeywords(LinearLayout llKeyword, List<String> keywords) {

        llKeyword.setVisibility(View.VISIBLE);
        llKeyword.removeAllViews();
        List<String> keywordsList = keywords;

        int width = 0;
        LinearLayout parent = null;
        for (int i = 0; i < keywordsList.size(); i++) {

            if (width == 0) {
                parent = new LinearLayout(MainActivity.this);
                parent.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                parent.setOrientation(LinearLayout.HORIZONTAL);
                parent.setPadding(15, 0, 15, 0);
                measureView(parent);
            }

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT));
            params.setMargins(0, 5, 15, 5);

            View someLayoutView = LayoutInflater.from(MainActivity.this).inflate(
                    R.layout.view_textview, null);
            final TextView tvKeyword = (TextView) someLayoutView.findViewById(R.id.tvName);
            tvKeyword.setText(keywordsList.get(i));
            //tvKeyword.setText(R.string.txt_period_location);
            tvKeyword.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_stroke_white));
            tvKeyword.setLayoutParams(params);

            measureView(tvKeyword);

            width += tvKeyword.getMeasuredWidth() + 5;

            Log.e("", "textview width " + tvKeyword.getMeasuredWidth()
                    + " width " + width);
            if (width < getWidth())
                parent.addView(tvKeyword);
            else {
                llKeyword.addView(parent);
                width = 0;
                parent = new LinearLayout(MainActivity.this);
                parent.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                parent.setOrientation(LinearLayout.HORIZONTAL);
                measureView(parent);

                width += tvKeyword.getMeasuredWidth();
                parent.addView(tvKeyword);
            }

            if (i == keywordsList.size() - 1)
                llKeyword.addView(parent);
        }

    }

    protected int getWidth() {
        WindowManager wm = (WindowManager) MainActivity.this.getSystemService(
                Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        Log.e("", "Width " + display.getWidth());
        return display.getWidth() - 80;
    }

    protected void measureView(View view) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        int childWidth = ViewGroup.getChildMeasureSpec(0, view.getPaddingLeft()
                + view.getPaddingRight(), params.width);
        int childHeight = ViewGroup.getChildMeasureSpec(0,
                view.getPaddingBottom() + view.getPaddingTop(), params.height);
        view.measure(childWidth, childHeight);
    }

4)现在使用如下方法:

LinearLayout llKeyword = findViewById(R.id.llKeyword);//find linear layout

 setKeywords(llKeyword, keywords);// call this method

5)编辑:用于制作丰富多彩的背景。制作一个可绘制文件 btn_bgsolid_blue.xml view_textviews.xml 文件的后台添加以下drawable:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#47bee6"></solid>
    <corners android:radius="15dp"></corners>
</shape>