动态添加TextView到Android视图

时间:2015-08-22 05:54:58

标签: android

我想动态地在布局中显示多个文本视图。 假设,用户从下拉菜单中选择10个项目,然后我可以一个接一个地显示它到布局。我需要与附加图像相同的视图 - https://youtrack.jetbrains.com/issues/RSRP

enter image description here

我在列表中选择了项目,现在我没有得到如何以所需方式显示数据。当我尝试列表视图时,所有数据都是一个接一个地垂直显示。 当我使用Linearlayout时,可以以水平或垂直顺序添加数据。所以我没有得到正确的数据显示方式。

请帮我解决这个问题。

提前致谢。

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

通常情况下,如果您使用水平方向的LinearLayout,它会根据您的需要放置您的项目。

答案 2 :(得分:0)

尝试这个..我习惯使用自定义视图。如果你没关系,试试吧..

自定义视图类是。

    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;
       }