流失布局

自定义view

public class flowLayout extends ViewGroup{

    public flowLayout(Context context) {
        this(context,null);
    }

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

    public flowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        //测量所有子view的宽高
        measureChildren(0,0);
        int totalWidth = 0;
        int totalHeight = 0;
        //循环遍历所有的view
        for (int i = 0; i < getChildCount(); i++) {
            View view = getChildAt(i);
            if (totalWidth + view.getMeasuredWidth() >= getMeasuredWidth()){
                totalWidth = 0;
                totalHeight += view.getMeasuredHeight();
            }
            view.layout(totalWidth,
                        totalHeight,
                        totalWidth+view.getMeasuredWidth(),
                        totalHeight+view.getMeasuredHeight());

            totalWidth += view.getMeasuredWidth();
        }
    }
}

activity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group);
        flLayout = findViewById(R.id.flow_layout);
        edText = findViewById(R.id.ed_text);
        btnSearch = findViewById(R.id.btn_search);

       // for (int i = 0; i < 20; i++) {
            btnSearch.setOnClickListener(this);
        //}
        }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_search:
                txts = new TextView(this);
                result = edText.getText().toString();
                ViewGroup.MarginLayoutParams layoutParams = new ViewGroup.
                        MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                txts.setPadding(10, 10, 10, 10);
                layoutParams.setMargins(60, 20, 55, 20);
                txts.setLayoutParams(layoutParams);
                txts.setText(result);
                txts.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        flLayout.removeView(txts);
                    }
                });
                flLayout.addView(txts);
        }
    }
}

xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.windows.month1025.GroupActivity">

    <LinearLayout
        android:padding="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText
            android:id="@+id/ed_text"
            android:layout_weight="3"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/btn_search"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="搜索"/>
    </LinearLayout>
    <com.example.windows.month1025.display.flowLayout
        android:id="@+id/flow_layout"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></com.example.windows.month1025.display.flowLayout>

</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值