Android 将视图添加到水平线布局中,并动态地在相邻和下方进行布局

Android 将视图添加到水平线布局中,并动态地在相邻和下方进行布局,android,android-linearlayout,Android,Android Linearlayout,我有一个recyclerview,其中我必须显示标题和描述。描述选项由n个项目(文本视图)组成,这些项目将动态地彼此相邻添加,如图所示。但是,如果项目宽度大于屏幕宽度,则应转到下一行 目前我有一个水平线性布局,如果没有足够的空间,我想在新行上添加新项目(TextView)。我希望输出像图2一样 我如何实现它?有没有更好的方法来做到这一点 有没有办法在不向hirarchy添加视图的情况下检查TextView屏幕宽度 1)在xml布局中创建线性布局,如下所示: <LinearLayout

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

目前我有一个水平线性布局,如果没有足够的空间,我想在新行上添加新项目(TextView)。我希望输出像图2一样

我如何实现它?有没有更好的方法来做到这一点

有没有办法在不向hirarchy添加视图的情况下检查TextView屏幕宽度

1)在xml布局中创建线性布局,如下所示:

<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>
5) 编辑:用于制作彩色背景。制作一个可绘制文件btn\u bgsolid\u blue.xml查看\u textviews.xml文件的背景中添加以下可绘制文件:

<?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>

若你们能使用这个库,那个么有很多,试试谷歌TagView
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);
    }
LinearLayout llKeyword = findViewById(R.id.llKeyword);//find linear layout

 setKeywords(llKeyword, keywords);// call this method
<?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>