自定义组合控件ViewGroup

本文介绍了一种自定义ViewGroup的实现方式,该组件能够自动调整子视图的位置,实现特定布局效果。通过重写onMeasure和onLayout方法,使得子视图可以按照预设规则进行排列。

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

工具类的自定义控件

package com.example.customview.view;


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


public class MyViewGroup extends ViewGroup {

private int selfWidth;
private int selfHeight;


public MyViewGroup(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}


public MyViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}


public MyViewGroup(Context context) {
super(context);
// TODO Auto-generated constructor stub
}


@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
//把里面的控件居中显示
/*
* 拿到儿子的数量
* 然后遍历,一个一个拿儿子
*/
//int childCount = getChildCount();//拿到孩子的个数
if (getChildAt(0)!=null) {
View child = getChildAt(0);//拿到第一个孩子
child.layout(0, 0, child.getMeasuredWidth(), child.getMeasuredHeight());
}
if (getChildAt(1)!=null) {
View child = getChildAt(1);//拿到第一个孩子
child.layout(selfWidth-child.getMeasuredWidth(), 0, selfWidth, child.getMeasuredHeight());
}
if (getChildAt(2)!=null) {
View child = getChildAt(2);//拿到第一个孩子
child.layout(0, selfHeight-child.getMeasuredHeight(), child.getMeasuredWidth(),selfHeight);
}
if (getChildAt(3)!=null) {
View child = getChildAt(3);//拿到第一个孩子
child.layout(selfWidth-child.getMeasuredWidth(), selfHeight-child.getMeasuredHeight(), selfWidth,selfHeight);
}


// for (int i = 0; i < childCount; i++) {
// View childAt = this.getChildAt(i);
// int childHeight = childAt.getMeasuredHeight();//拿到孩子的高
// int childWidth = childAt.getMeasuredWidth();//拿到孩子的宽
// childAt.layout((selfWidth-childWidth)/2,//左
// (selfHeight-childHeight)/2,//上
// (selfWidth+childWidth)/2,//右
// (selfHeight+childHeight)/2);//下
// }




}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//①保存自己的宽高
saveSelfDimension(widthMeasureSpec, heightMeasureSpec);
//②测量孩子的宽高
measureChildren(widthMeasureSpec, heightMeasureSpec);
}


private void saveSelfDimension(int widthMeasureSpec, int heightMeasureSpec) {
selfWidth = 0;
selfHeight = 0;
//得到测量模式,判断 如果是前两种  值就是正确的值,如果是后一种,就是错误的值,值就是0 
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heigthMode = MeasureSpec.getMode(heightMeasureSpec);
if (widthMode==MeasureSpec.AT_MOST||widthMode==MeasureSpec.EXACTLY) {
//得到自己的尺寸,并且把尺寸赋值给我定义好的变量 
selfWidth=MeasureSpec.getSize(widthMeasureSpec);
}else {
selfWidth=0;
}
//设置高
if (heigthMode==MeasureSpec.AT_MOST||heigthMode==MeasureSpec.EXACTLY) {

//得到自己的尺寸,并且把尺寸赋值给我定义好的变量 
selfHeight=MeasureSpec.getSize(heightMeasureSpec);
}else {
selfHeight=0;
}
//需要保存一下测量的宽高
this.setMeasuredDimension(selfWidth, selfHeight);
}

@Override
protected void onDraw(Canvas canvas) { 
super.onDraw(canvas);
}


}


之后直接在布局里面画界面

 <com.example.customview.view.MyViewGroup
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:textColor="#f00"
            android:text="哈哈哈哈"
            />
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"            
            />
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="哈哈哈哈"
            />
        
    </com.example.customview.view.MyViewGroup>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值