Android流式布局FlowLayout

大致效果如下
在这里插入图片描述
布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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"
    tools:context=".MainActivity">

    <com.example.day4.myfloat.MyFloatLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/MyFloat_Layout"></com.example.day4.myfloat.MyFloatLayout>

</android.support.constraint.ConstraintLayout >

创建FlowLayout类 继承LinearLayout

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.day4.R;

/**
 * 获取两种屏幕宽和高的方式
 * 最常用的是 DisplayMetrics
 * WindowManager 不常用了 我查看已经加上删除线 过时
 */
public class MyFloatLayout extends LinearLayout {

    private int mScreenWidth;

    public MyFloatLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
//        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
//        Display display = manager.getDefaultDisplay();
//        display.getWidth();
        mScreenWidth = metrics.widthPixels;
        //设置这个布局垂直显示
        setOrientation(VERTICAL);
    }

    public void setData(String[] datas) {
        LinearLayout linearLayout = getLin();
        for (int i = 0; i < datas.length; i++) {
            String data = datas[i];
            int numWidth = 0;
            //得到一行LinearLayout到底有多少子控件 因为我要计算每个子控件加在一起的宽度
            int childCount = linearLayout.getChildCount();
            //这个for循环只是计算一行LinearLayout的所有子控件的宽的和
            for (int j = 0; j < childCount; j++) {
                //通过index得到每一个子控件
                TextView tv = (TextView) linearLayout.getChildAt(j);
                LayoutParams layoutParams = (LayoutParams) tv.getLayoutParams();
                int leftMargin = layoutParams.leftMargin;
                //测量这个tv的高和宽
                tv.measure(getMeasuredWidth(), getMeasuredHeight());
                numWidth += tv.getMeasuredWidth() + leftMargin + tv.getPaddingLeft() + tv.getPaddingRight();
            }
            TextView dataText = getText();
            //设置属性
            LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            params.leftMargin = 10;
            params.topMargin = 10;
            dataText.setLayoutParams(params);
            dataText.setText(data);
            dataText.measure(getMeasuredWidth(), getMeasuredHeight());
            int dataTextWidth = dataText.getMeasuredWidth() + dataText.getPaddingLeft() + dataText.getPaddingRight();
            //考虑到一个字符串很长,就直接超过整个屏幕的高了
            if (dataTextWidth >= mScreenWidth) {
                String s = data.substring(0, 4);
                dataText.setText(s + "...");
                dataText.measure(getMeasuredWidth(), getMeasuredHeight());
                dataTextWidth = dataText.getMeasuredWidth();
            }
            if (mScreenWidth >= numWidth + dataTextWidth) {
                linearLayout.addView(dataText);
            } else {
                //这里面对LinearLayout重新赋值 通过getLin换行
                linearLayout = getLin();
                linearLayout.addView(dataText);
            }
        }
    }

    private TextView getText() {
        TextView textView = new TextView(getContext());
        textView.setTextSize(16);
        textView.setTextColor(Color.BLACK);
        //在drawable中创建样式 圆角描边
        textView.setBackgroundResource(R.drawable.text_style);
        textView.setPadding(10, 5, 10, 5);
        return textView;
    }

    //初始化子LinearLayout
    private LinearLayout getLin() {
        LinearLayout linearLayout = new LinearLayout(getContext());
        //LayoutParams控制组件大小的一个工具类
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        linearLayout.setLayoutParams(params);
        //this本类对象
        this.addView(linearLayout);//只要重新添加View了自动换行了
        return linearLayout;
    }
}

圆角描边设置
在这里插入图片描述
在MainActivity中模拟数据 传入FlowLayout中

package com.example.day4;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.example.day4.myfloat.MyFloatLayout;

public class MainActivity extends AppCompatActivity {
    private String[] data = {"流感", "咳嗽", "过敏", "发烧", "感冒", "湿疹", "便秘", "痔疮", "协和", "鼻炎", "失眠", "痛风", "上火", "脚气", "抑郁症", "性欲", "乳腺增生", "头晕", "腰痛"};
    private MyFloatLayout MyFloat_Layout;

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

    private void initView() {
        MyFloat_Layout = (MyFloatLayout) findViewById(R.id.MyFloat_Layout);
        MyFloat_Layout.setData(data);
    }
}

以上就是一个单纯简单的流式布局 无任何其他功能

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值