流式布局

布局

    <EditText
        android:id="@+id/edit_keys"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/btn_add"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:text="搜索"/>
</LinearLayout>


<com.lll.fanview02.FlowLayout
    android:id="@+id/flow_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="apple"
        android:textSize="20sp"
        android:background="@drawable/btn_bg"/>
</com.lll.fanview02.FlowLayout>



<Button
    android:id="@+id/clear"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:text="清除历史记录"/>

flow_item页面

<?xml version="1.0" encoding="utf-8"?>

shape

<?xml version="1.0" encoding="utf-8"?>

<stroke
    android:color="@color/colorPrimary"
    android:width="2dp"></stroke>

FlowLayout页面

package com.lll.fanview02;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FlowLayout extends FrameLayout {
//水平间的距离
private final static int H_DISTANCE = 20;
//竖直间的距离
private final static int V_DISTANCE = 20;

public FlowLayout(Context context) {
    super(context);
}

public FlowLayout(Context context,AttributeSet attrs) {
    super(context, attrs);
}

public FlowLayout(Context context,AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}
public void addTextView(String keys){
    //加载页面布局
   TextView textView = (TextView) View.inflate(getContext(),R.layout.flow_item,null);
   textView.setText(keys);
   //布局宽高自适应
    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
    //设置控件的布局参数
    textView.setLayoutParams(params);

    this.addView(textView);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    //获取本控件的宽度
    int width = getWidth();
    //行数
    int row = 0;
    int disWidth = H_DISTANCE;//子控件左边的坐标
    for (int i=0;i<getChildCount();i++){
        //查找子控件
        View view = getChildAt(i);
        //获取控件的宽高
        int viewWidth = view.getWidth();
        int viewHeight = view.getHeight();
        //判断控件是否超过了屏幕宽度
        if (disWidth+viewWidth > width){
            //行数增加
            row++;
            //初始化坐标
            disWidth = H_DISTANCE;
        }
        int viewTop = row * viewHeight + row * V_DISTANCE;
        //子布局控件
        view.layout(disWidth,viewTop,disWidth+viewWidth,viewTop+viewHeight);
        //记录下一次子控件左边的坐标
        disWidth += (viewWidth+H_DISTANCE);
    }
}

}

MainActivity页面

package com.lll.fanview02;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

private FlowLayout flow;
private Button btn;
private EditText edit;
private Button clear;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //找控件
    flow = findViewById(R.id.flow_layout);
    btn = findViewById(R.id.btn_add);
    edit = findViewById(R.id.edit_keys);
    clear = findViewById(R.id.clear);
    btn.setOnClickListener(this);
    clear.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.btn_add:
            String keys = edit.getText().toString();
            flow.addTextView(keys);
            break;
        case R.id.clear:
            flow.removeAllViews();
            break;
    }
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值