流式布局

自定义 布局;;;;

package soexample.umeng.com.zfx001;

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

/**
 * Created by TP-PC on 2018.10.25.
 */

public class Stream extends ViewGroup {

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

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

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

    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(View v, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @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);
            // View添加点击事件
            final int position = i;
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onItemClick(v, position);
                    }
                }
            });
            // 当前view的宽度
            int vw = view.getMeasuredWidth();
            // 当前view的高度
            int vh = view.getMeasuredHeight();
            // 如果总的View宽度加上当前View的宽度大于控件的宽度
            if (totalWidth + vw > getMeasuredWidth()) {
                // 执行换行操作,总宽度设置为0,高度加上当前view的高度
                totalWidth = 0;
                totalHeight += vh;
            }
            view.layout(totalWidth, totalHeight, totalWidth + vw,
                    totalHeight + vh);
            // 总的宽度在原有宽度的基础上加上当前view的宽度
            totalWidth += vw;
        }
    }
}

 

自定义布局样式;;;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_attr"
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    
</LinearLayout>

 

Activity布局;;;;;

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/txt_search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="搜索" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/txt_search"
            android:orientation="horizontal"
            android:padding="5dp">

            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:src="@mipmap/ic_launcher" />

            <EditText
                android:id="@+id/et_search"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@null" />
        </LinearLayout>
    </RelativeLayout>

    <soexample.umeng.com.zfx001.Stream
        android:id="@+id/fl_search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></soexample.umeng.com.zfx001.Stream>
</LinearLayout>

 

页面;;;;;

package soexample.umeng.com.zfx001;

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

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

public class StreamLinearlayout extends AppCompatActivity implements View.OnClickListener {

    private TextView txtSearch;
    private EditText etSearch;
    private Stream flSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stream_linearlayout);

        txtSearch = findViewById(R.id.txt_search);
        etSearch = findViewById(R.id.et_search);
        flSearch = findViewById(R.id.fl_search);

        txtSearch.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.txt_search:
                String text = etSearch.getText().toString().trim();
                if (!TextUtils.isEmpty(text)) {
                    TextView txt = new TextView(this);
                    txt.setText(text);
                    txt.setPadding(10, 10, 10, 10);
                    ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    txt.setLayoutParams(layoutParams);
                    flSearch.addView(txt);
                    etSearch.setText("");
                }
                break;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值