流式布局

package com.example.liuhuan20181025;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by 。。。。 on 2018/10/25.
 */

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) {
    measureChildren(0, 0);
    int totalWidth = 0;
    int totalHeight = 0;
    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();
    }
}

}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText
            android:id="@+id/et_text"
            android:layout_width="0dp"
            android:layout_weight="5"
            android:background="@drawable/radio"
            android:layout_height="wrap_content"></EditText>
        <Button
            android:id="@+id/btn_add"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="wrap_content"
            android:text="搜索"/>
        <Button
            android:id="@+id/btn_decrease"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="wrap_content"
            android:text="清除"/>
    </LinearLayout>
    <com.example.liuhuan20181025.FlowLayout
        android:id="@+id/flow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></com.example.liuhuan20181025.FlowLayout>

    <!--<TextView-->
        <!--android:id="@+id/home_text"-->
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="match_parent" />-->
    <!--<com.recker.flybanner.FlyBanner-->
        <!--android:id="@+id/images"-->
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="wrap_content"></com.recker.flybanner.FlyBanner>-->

</LinearLayout>

package com.example.liuhuan20181025;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SearchView;
import android.widget.TextView;

import com.recker.flybanner.FlyBanner;

import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends AppCompatActivity {

    private FlyBanner banner;
    private List<String> list;
    private EditText searchView;
    private Button btnadd;
    private Button btndecrease;
    private FlowLayout fl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        searchView = findViewById(R.id.et_text);
        fl = findViewById(R.id.flow);
        btnadd = findViewById(R.id.btn_add);
        btndecrease = findViewById(R.id.btn_decrease);
        addHistor();
        btnadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addHistor();
            }
        });
        btndecrease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fl.removeAllViews();
            }
        });
//        banner = findViewById(R.id.images);
//        list = new ArrayList<>();
//        list.add("http://www.zhaoapi.cn/images/quarter/ad1.png");
//        list.add("http://www.zhaoapi.cn/images/quarter/ad2.png");
//        list.add("http://www.zhaoapi.cn/images/quarter/ad3.png");
//        list.add("http://www.zhaoapi.cn/images/quarter/ad4.png");
//        banner.setImagesUrl(list);
    }

    private void addHistor() {
        TextView txt=new TextView(this);
        String s=searchView.getText().toString();
        if(!TextUtils.isEmpty(s)){
            txt.setText(""+s);
            txt.setPadding(15,15,15,15);
            fl.addView(txt);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值