动态将TextView添加到Android视图

最后发布: 2015-08-22 05:54:57


问题

我想在布局中动态显示多个文本视图。 假设,用户从下拉菜单中选择了10个项目,那么我可以将其显示为一个接一个地布局。我需要与附加图像相同的视图- 在此处输入图片说明

在此处输入图片说明

我已经在列表中选择了项目,但现在我还没有获得如何以期望的方式显示数据。当我尝试列表视图时,所有数据都垂直显示。 当我使用Linearlayout时,可以按水平或垂直顺序添加数据。因此,我无法获得显示数据的正确方法。

请帮助我解决这个问题。

提前致谢。

android
回答

您要寻找的是芯片。为什么不使用材料设计芯片,您可以在这里参考。 http://www.google.com/design/spec/components/chips.html#
您可以像使用网格视图一样实现,如果您对使用芯片不感兴趣的话,只需拥有一行网格视图并在其中填充一个单元格即可


回答

通常,如果您将LinearLayout与水平方向配合使用,它将根据需要放置您的项目。


回答

尝试这个..我曾经使用自定义视图来实现。如果您还可以,请尝试一下。

自定义视图类是。

    public class WrapLayout extends ViewGroup {

    private int paddingHorizontal;
    private int paddingVertical;

    public WrapLayout(Context context) {
        super(context);
        init();
    }

    public WrapLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WrapLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        paddingHorizontal = getResources().getDimensionPixelSize(10);
        paddingVertical = getResources().getDimensionPixelSize(10);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int childLeft = getPaddingLeft();
        int childTop = getPaddingTop();
        int lineHeight = 0;
        // 100 is a dummy number, widthMeasureSpec should always be EXACTLY for FlowLayout
        int myWidth = resolveSize(100, widthMeasureSpec);
        int wantedHeight = 0;
        for (int i = 0; i < getChildCount(); i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() == View.GONE) {
                continue;
            }
            // let the child measure itself
            child.measure(
                    getChildMeasureSpec(widthMeasureSpec, 0, child.getLayoutParams().width),
                    getChildMeasureSpec(heightMeasureSpec, 0, child.getLayoutParams().height));
            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();
            // lineheight is the height of current line, should be the height of the heightest view
            lineHeight = Math.max(childHeight, lineHeight);
            if (childWidth + childLeft + getPaddingRight() > myWidth) {
                // wrap this line
                childLeft = getPaddingLeft();
                childTop += paddingVertical + lineHeight;
                lineHeight = childHeight;
            }
            childLeft += childWidth + paddingHorizontal;
        }
        wantedHeight += childTop + lineHeight + getPaddingBottom();
        setMeasuredDimension(myWidth, resolveSize(wantedHeight, heightMeasureSpec));
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        int childLeft = getPaddingLeft();
        int childTop = getPaddingTop();
        int lineHeight = 0;
        int myWidth = right - left;
        for (int i = 0; i < getChildCount(); i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() == View.GONE) {
                continue;
            }
            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();
            lineHeight = Math.max(childHeight, lineHeight);
            if (childWidth + childLeft + getPaddingRight() > myWidth) {
                childLeft = getPaddingLeft();
                childTop += paddingVertical + lineHeight;
                lineHeight = childHeight;
            }
            child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
            childLeft += childWidth + paddingHorizontal;
        }
    }
    }

而我的xml是:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText_sample"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add It"
        android:id="@+id/addit"
         />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
          <!-- i mean where you put the WrapLayout class in your project that package name -->
        <your.package.name.WrapLayout
            android:id="@+id/flow_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="20dp"/>
    </ScrollView>
    </LinearLayout>

最后是MainActivity

        ViewGroup flowContainer;

在OnCreate中

        flowContainer= (ViewGroup) findViewById(R.id.flow_container);
        final EditText text=(EditText)findViewById(R.id.editText_sample);
        Button addIT=(Button)findViewById(R.id.addit);
        addIT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String data=text.getText().toString();
                flowContainer.addView(createTextView(data),
                        new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            }
        });

在mainactivity中复制此功能

       private View createTextView(String text) {
        TextView textView = new TextView(this);
        textView.setText(text);
        textView.setGravity(Gravity.CENTER);                     
         textView.setCompoundDrawablesWithIntrinsicBounds(0,0,R.mipmap.ic_launcher,0);
        return textView;
       }