【达内课程】自定义控件(居中)

这一节自定义一个容器,新建一个CenterLayout继承自ViewGroup

先做一些练习

模拟一个LinearLayout

//写一个布局,容器
public class CenterLayout extends ViewGroup {
    public CenterLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    //设置子控件如何显示
    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        View view1 = getChildAt(0);
        Log.d("CenterLayout","textView width:"+view1.getMeasuredWidth()+";height:"+view1.getMeasuredHeight());
        //指定控件位置
        view1.layout(0,0,600,100);

        View view2 = getChildAt(1);
        Log.d("CenterLayout","image width:"+view2.getMeasuredWidth()+";height:"+view2.getMeasuredHeight());
        view2.layout(0,100,600,300);
    }

    //测量
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //获得子控件个数
        int childViewCount = getChildCount();
        //测量
        for(int i=0;i<childViewCount;i++){
            View childView = getChildAt(i);
            childView.measure(widthMeasureSpec,heightMeasureSpec);
        }
    }
}

布局

<com.xx.shadeview.CenterLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:Shade="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.xx.shadeview.ShadeView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        Shade:shade_color="#FF888888"
        Shade:text="运动伙伴 就选酷跑"
        Shade:text_color="#FF0000FF"
        Shade:text_size="20" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="运动伙伴 就选酷跑"
        android:textSize="20dp" />

</com.xx.shadeview.CenterLayout>

这里的ShadeView,是之前的自定义控件【达内课程】自定义控件(文字阴影)

效果
在这里插入图片描述
现在模拟一个帧布局

		View view1 = getChildAt(0);
        view1.layout(0,0,300,100);
        //坐标左上角重叠
        View view2 = getChildAt(1);
        view2.layout(0,0,600,100);

效果
在这里插入图片描述
模拟一个相对布局

        View view1 = getChildAt(0);
        view1.layout(0,0,300,100);

        View view2 = getChildAt(1);
        view2.layout(300,100,600,200);

效果
在这里插入图片描述
现在来做居中效果

在这里插入图片描述

假设有2个view,来计算下它们相关数据
在这里插入图片描述
CenterLayout

//写一个布局,容器
public class CenterLayout extends ViewGroup {
    int groupWidth,groupHeight;

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        groupHeight = h;
        groupWidth = w;
    }

    public CenterLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    //设置子控件如何显示
    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        //获得子控件个数
        int childViewCount = getChildCount();

        //所有控件的高度之和
        int sum = 0;
        for(int j=0;j<childViewCount;j++){
            View view = getChildAt(j);
            sum+=view.getMeasuredHeight();
        }
        int top = (groupHeight - sum)/2;

        for(int j=0;j<childViewCount;j++){
            View view = getChildAt(j);
            int viewWidth = view.getMeasuredWidth();
            int viewHeight = view.getMeasuredHeight();
            int left = (groupWidth - viewWidth)/2;
            int right = left + viewWidth;
            int bottom = top + viewHeight;

            view.layout(left,top,right,bottom);

            top += viewHeight;
        }
    }

    //测量
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //获得子控件个数
        int childViewCount = getChildCount();
        //测量
        for(int i=0;i<childViewCount;i++){
            View childView = getChildAt(i);
            childView.measure(widthMeasureSpec,heightMeasureSpec);
        }
    }
}

布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.xx.shadeview.CenterLayout xmlns:Shade="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.xx.shadeview.ShadeView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            Shade:shade_color="#FF888888"
            Shade:text="运动伙伴 就选酷跑"
            Shade:text_color="#FF0000FF"
            Shade:text_size="20" />

        <com.xx.shadeview.ShadeView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            Shade:shade_color="#FF888888"
            Shade:text="运动伙伴 就选酷跑"
            Shade:text_color="#FF0000FF"
            Shade:text_size="20" />

    </com.xx.shadeview.CenterLayout>
</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值