您的位置:首页 > 其它

自定义组合控件ViewGroup

2016-07-26 11:08 260 查看




工具类的自定义控件

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 widthMeasureSp
4000
ec, 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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: