自定义view中的onLayout

本文介绍了一种自定义的Android ViewGroup实现方式,该ViewGroup能够让内部的Button等子视图水平排列并居中显示。文章提供了详细的源码解析,包括如何获取屏幕尺寸、测量和定位子视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

xml文件中

<com.example.text.MyViewGroup
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="测试一" >
    </Button>

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="测试二" >
    </Button>

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="测试三" >
    </Button>

    <Button
        android:id="@+id/btn4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="测试四" >
    </Button>

</com.example.text.MyViewGroup>


自定义类,继承ViewGroup


import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
 
public class MyViewGroup extends ViewGroup {
 
    private Context mContext;
    private int sreenH;
 
    public MyViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        // 获取屏幕的高度
        sreenH = getScreenSize(((Activity) mContext))[1];
    }
    
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
        // 测量子View
        measureChildren(widthMeasureSpec, heightMeasureSpec);
    }
 
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // 获得子View个数
        int childCount = getChildCount();
        // 设置一个变量保存到父View左侧的距离
        int mLeft = 0;
        // 遍历子View
        for (int i = 0; i < childCount; i++) {
 
            View childView = getChildAt(i);
            // 获得子View的高度
            int childViewHeight = childView.getMeasuredHeight();
            // 获得子View的宽度
            int childViewWidth = childView.getMeasuredWidth();
            // 让子View在竖直方向上显示在屏幕的中间位置
            int height = sreenH / 2 - childViewHeight / 2;
            // 调用layout给每一个子View设定位置mLeft,mTop,mRight,mBottom.左上右下
            childView.layout(mLeft, height, mLeft + childViewWidth, height
                    + childViewHeight);
            // 改变下一个子View到父View左侧的距离
            mLeft += childViewWidth;
        }
    }
 
    /**
     * 获取屏幕尺寸
     */
    public static int[] getScreenSize(Activity activity) {
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        return new int[] { metrics.widthPixels, metrics.heightPixels };
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值