自定义View的理解注释

自定义的代码文件

public class MyLinearlayout extends ViewGroup {

    private int marginleft=20;
    private int margintop=10;

    //默认为0 纵向  1横向  2斜
    private int orientation=0;


    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, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        //获取自定义orientation是 0 还是1  还是2
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyLinearlayout);
        orientation = typedArray.getInteger(0, 0);
    }


    //测量
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

       int totalHeight=0;

        //测量子类位置
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        //测量高
        //高的相关模式
        int modeheight = MeasureSpec.getMode(heightMeasureSpec);
        //高的相关具体大小
        int sizeheight = MeasureSpec.getMode(heightMeasureSpec);

        //测量宽
        //高的相关模式
        int modewidth = MeasureSpec.getMode(widthMeasureSpec);
        //高的相关具体大小
        int sizewidth = MeasureSpec.getMode(widthMeasureSpec);

        if(orientation == 0) {
            //三种模式
            switch (modeheight) {
                //对应自适应
                case MeasureSpec.AT_MOST:

                    int A = 0;
                    for (int i = 0; i < getChildCount(); i++) {
                        A += getChildAt(i).getMeasuredHeight();
                    }
                    totalHeight = A + getChildAt(0).getMeasuredHeight();
                    sizewidth = 300;
                    break;


                //对应固定dp精确值
                case MeasureSpec.EXACTLY:
                    totalHeight = sizeheight;
                    sizewidth = 200;
                    break;
                //对应全包match_parent
                case MeasureSpec.UNSPECIFIED:
                    break;
            }

            //单位为像素
            setMeasuredDimension(sizewidth, totalHeight);
        }
    }

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

        //子控件的位置锁定
        int top=margintop;
        for (int x = 0; x < getChildCount(); x++) {
            View childAt = getChildAt(x);
            childAt.layout(marginleft, top, childAt.getMeasuredWidth() + marginleft, childAt.getMeasuredHeight()+top);
            top+=childAt.getMeasuredHeight()+margintop;
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }
}
布局文件

<?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:orientation="vertical"
    tools:context="com.example.xsj.zidingyiview.MainActivity">

    <com.example.xsj.zidingyiview.MyLinearlayout
        android:background="#c1a9a2"
        android:layout_width="200dp"
        orientation="vertical"
        android:layout_height="wrap_content">
        
        <TextView
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="#ff3600"
            android:text="小礼物走一圈"
            android:gravity="center"
            />

        <TextView
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="#734a3f"
            android:text="小礼物走二圈"
            />

        <TextView
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="#7b9c47"
            android:text="小礼物走三圈"
            />

        <TextView
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:background="#7b9c47"
            android:text="小礼物走四圈"
            />
    </com.example.xsj.zidingyiview.MyLinearlayout>
</LinearLayout>

在values文件夹下自定义orientation展示格式 --attrs

<resources>
    <declare-styleable name="MyLinearlayout">
        <attr name="orientation" format="enum">
            <enum value="0" name="horizontal"></enum>
            <enum value="1" name="vertical"></enum>
            <enum value="2" name="all"></enum>
        </attr>
    </declare-styleable>
</resources>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值