自定义阶梯布局

FlowLayout中代码:
package com.example.zhoukao1_moni;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by DELL on 2018/6/9.
 */

public class FlowLayout extends ViewGroup {



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

    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 onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        measureChildren(widthMeasureSpec,heightMeasureSpec);

        int widthsize = MeasureSpec.getSize(widthMeasureSpec);
        int widthmode = MeasureSpec.getMode(widthMeasureSpec);
        int heightsize = MeasureSpec.getSize(heightMeasureSpec);
        int heightmode = MeasureSpec.getMode(heightMeasureSpec);

        int width = 0;
        int height = 0;
        int lineWidth = 0;
        int lineHeight = 0;
        int childWidth = 0;
        int childHeight = 0;
        int totalHeight = 0;
        View childView;


        for (int i = 0; i < getChildCount(); i++) {

            childView = getChildAt(i);

            //获取子view的宽高
            childWidth = childView.getMeasuredWidth();
            width = 2 * childWidth;

            childHeight = childView.getMeasuredHeight();

            if(childWidth > widthsize){
                throw  new IllegalArgumentException("子view的宽度不能超过父控件的宽度");
            }

            if( i % 2 == 0){
                totalHeight += lineHeight;

                lineHeight = childHeight;
                lineWidth = childWidth;
            }
            else{
                totalHeight += lineHeight;
                lineWidth += childWidth;
                lineHeight = childHeight;
            }

            if(i == getChildCount() - 1){
                totalHeight += lineHeight;
                height = totalHeight;
            }
        }

        width = widthmode == MeasureSpec.EXACTLY ? widthsize : width;
        height = heightmode == MeasureSpec.EXACTLY ? heightsize : height;

        setMeasuredDimension(width,height);
    }

    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {

        int lineWidth = 0;
        int lineHeight = 0;
        int childWidth = 0;
        int childHeight = 0;
        int totalHeight = 0;
        View childView;

        for (int j = 0; j < getChildCount(); j++) {

            childView = getChildAt(j);

            childWidth = childView.getMeasuredWidth();
            childHeight = childView.getMeasuredHeight();

            if(j % 2 == 0){
                totalHeight += lineHeight;
                lineWidth = 0;
                childViewLayout(childView,lineWidth,totalHeight,lineWidth+childWidth,totalHeight+childHeight);
                lineWidth = childWidth;
                lineHeight = childHeight;
            }
            else{
                totalHeight += lineHeight;
                childViewLayout(childView,lineWidth,totalHeight,lineWidth+childWidth,totalHeight+childHeight);
                lineWidth += childWidth;
                lineHeight = childHeight;
            }
        }

    }

    public void childViewLayout(View childView,int l,int t,int r,int b){
        childView.layout(l,t,r,b);
    }

}

MainActivity中代码:

package com.example.zhoukao1_moni;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private MyTitleView titleview;
    private FlowLayout flowlayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        titleview = findViewById(R.id.titleview);
        flowlayout = findViewById(R.id.flowlayout);

        titleview.setmOnBtnClickListener(new MyTitleView.onBtnClickListener() {
            @Override
            public void onLeftBtnClick() {
                flowlayout.removeViewAt(0);
                //Toast.makeText(MainActivity.this, "------", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onRightBtnClick() {
                Button button = new Button(getApplicationContext());
                button.setBackgroundColor(Color.GREEN);
                ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.width = getWindowManager().getDefaultDisplay().getWidth()/2;
                layoutParams.height = getWindowManager().getDefaultDisplay().getHeight()/15;
                flowlayout.addView(button,layoutParams);
                //Toast.makeText(MainActivity.this, "++++++", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

猜你喜欢

转载自blog.csdn.net/gaoyiranblog/article/details/80643632