自定义控件 流式布局

 自定义控件 流式布局

package com.example.layout;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bwei.chenguoxing20181203.MainActivity;
import com.bwei.chenguoxing20181203.R;

public class FlowLayout extends FrameLayout {


    private int text_size;
    private int text_color;

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

    public FlowLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.flow);
        text_color = array.getInt(R.styleable.flow_text_color, 0xFF0000FF);
        text_size = array.getInt(R.styleable.flow_text_size, 0);
    }

    public FlowLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.flow);
        text_color = array.getInt(R.styleable.flow_text_color, 0xFF0000FF);
        text_size = array.getInt(R.styleable.flow_text_size, 0);

    }
    public void getData(final String data){
        TextView textView = (TextView) View.inflate(getContext(),R.layout.text_layout,null);
        textView.setText(data);
        textView.setTextColor(text_color);
        textView.setTextSize(text_size);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT);
        textView.setLayoutParams(layoutParams);
        addView(textView);
        textView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(),data,Toast.LENGTH_SHORT).show();
            }
        });
    }

    //重写onMeasure
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    //重写onDraw
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    //重写onLayout
    @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 = 20;
        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=20;
            }
            view.layout(disWidth,row*viewHeight,disWidth+viewWidth,(row+1)*viewHeight);
            disWidth+=viewWidth;
        }
    }
}

文字布局 

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="20dp"
    android:textSize="20dp"
    android:text="标题"
    >

自定义属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="flow">
        <attr name="text_size" format="integer"/>
        <attr name="text_color" format="integer"/>
    </declare-styleable>
</resources>

使用控件

<com.bwei.layout.FlowLayout
        android:id="@+id/flow_1"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:text_size="20"
        app:text_color="0xFFFF0000"
        >
    </com.bwei.layout.FlowLayout>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值