自定义View继承ViewGroup自定义属性,实现水平垂直阶梯的排列

垂直布局

水平布局

阶梯布局

先创建自定义布局的类 MyLinearLayout

public class MyLinearLayout extends ViewGroup{

    private int marginTop = 20;
    private int marginLeft = 20;
    private int initTop = 20;
    private int initLeft = 20;
    
     private int orientation;//0或1或2
    public MyLinearLayout(Context context) {
        this(context,null);
    }

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

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


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureChildren(widthMeasureSpec,heightMeasureSpec);

        //最终的宽和高
        int totalHeight = 0;
        int totalWidth = 0;

        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);

        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        if(orientation==0){//0为垂直 1为水平 2为阶梯
            switch (modeHeight){
                case MeasureSpec.EXACTLY:
                    totalHeight = sizeHeight;
                    totalWidth = sizeWidth;
                    break;
                case MeasureSpec.AT_MOST:
                    for (int i=0;i<getChildCount();i++){
                        View childAt = getChildAt(i);
                        totalHeight += childAt.getMeasuredHeight()+marginTop;

                    }
                    totalHeight+=initTop;
                    totalWidth = getChildAt(0).getMeasuredWidth()+marginLeft*2;
                    break;
                case MeasureSpec.UNSPECIFIED:
                    break;
            }
        }else if(orientation==1){//0为垂直 1为水平 2为阶梯
            switch (modeWidth){
                case MeasureSpec.EXACTLY:
                    totalWidth = sizeWidth;
                    totalHeight = sizeHeight;
                    break;
                case MeasureSpec.AT_MOST:
                    for (int i=0;i<getChildCount();i++){
                        View childAt = getChildAt(i);
                        totalWidth += childAt.getMeasuredWidth()+marginLeft;
                        // totalWidth = childAt.getMeasuredWidth()+marginLeft;
                    }
                    totalWidth+=initLeft;
                    totalHeight = getChildAt(0).getMeasuredHeight()+marginTop*2;
                    break;
                case MeasureSpec.UNSPECIFIED:
                    break;
            }
        }else if(orientation==2){//0为垂直 1为水平 2为阶梯
            switch (modeHeight){
                case MeasureSpec.EXACTLY:
                    totalHeight = sizeHeight;
                    totalWidth = sizeWidth;
                    break;
                case MeasureSpec.AT_MOST:
                    for (int i=0;i<getChildCount();i++){
                        View childAt = getChildAt(i);
                        totalHeight += childAt.getMeasuredHeight()+marginTop;

                    }
                    totalHeight+=initTop;
                    // totalWidth = getChildAt(0).getMeasuredWidth()+marginLeft*2;
                    break;
                case MeasureSpec.UNSPECIFIED:
                    break;
            }
            switch (modeWidth){
                case MeasureSpec.EXACTLY:
                    totalWidth = sizeWidth;
                    totalHeight = sizeHeight;
                    break;
                case MeasureSpec.AT_MOST:
                    for (int i=0;i<getChildCount();i++){
                        View childAt = getChildAt(i);
                        totalWidth += childAt.getMeasuredWidth()+marginLeft;
                        // totalWidth = childAt.getMeasuredWidth()+marginLeft;
                    }
                    totalWidth+=initLeft;
                   // totalHeight = getChildAt(0).getMeasuredHeight()+marginTop*2;
                    break;
                case MeasureSpec.UNSPECIFIED:
                    break;
            }

        }


        setMeasuredDimension(totalWidth,totalHeight);

    }

    /**
     * 必须重写的方法..确定view的位置
     * */
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

        int mTop = initTop;
        int mLeft = initLeft;
        int childCount = getChildCount();//拿到子控件的数量
        for (int i=0;i<childCount;i++){
            View childAt = getChildAt(i);//循环遍历中拿到当前的view
           // Log.e("myMessage","mTop = "+mTop);
            //此时是0,因为没有写onmeasure,,measureChildren(widthMeasureSpec,heightMeasureSpec);
           // childAt.layout(0,0,childAt.getMeasuredWidth(),childAt.getMeasuredHeight());
            //不重叠,拉开上下距离
      childAt.layout(mLeft,mTop,childAt.getMeasuredWidth()+mLeft,childAt.getMeasuredHeight()+mTop);
         if(orientation==0){//0为垂直
             mTop+=childAt.getMeasuredHeight()+marginTop;

         }else if(orientation==1){//1为水平
             mLeft+=childAt.getMeasuredWidth()+marginLeft;

         } if(orientation==2){//2为阶梯,斜对角
                mTop+=childAt.getMeasuredHeight()+marginTop;
                mLeft+=childAt.getMeasuredWidth()+marginLeft;
            }
        }

    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyLinearLayout);
        //orientation = 0,1,2  此时拿到了值
        orientation = typedArray.getInteger(0,0);
    }
}
在values下新建attrs.xml  (values-new-xml-values xml file)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyLinearLayout">
        <attr name="orientation" format="enum">
            <enum value="0" name="vertical"/>
            <enum value="1" name="horizontal"/>
            <enum value="2" name="all"/>
        </attr>
        <attr name="orientation1" format="integer"/>
    </declare-styleable>
</resources>
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:gravity="center"
    tools:context="com.example.day180110_customlinearlayout.MainActivity">

    <com.example.day180110_customlinearlayout.customview.MyLinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#a3e4d9"
        app:orientation="all"
        >

        <TextView
            android:gravity="center"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="#99dfc0ea"
            android:text="第一" />

        <TextView
            android:gravity="center"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="#99d599ea"
            android:text="第二" />

        <TextView
            android:gravity="center"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="#99c66ce6"
            android:text="第三" />

        <TextView
            android:gravity="center"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="#99bb43e7"
            android:text="第四" />
    </com.example.day180110_customlinearlayout.customview.MyLinearLayout>
</LinearLayout>







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值