自定义组合控件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>
相关文章推荐
- MongoDB 游标和索引
- Android Fragment使用详解及生命周期
- apache常用工作模式的区别
- 小编亲测杭州最火的海南鸡饭,最好吃的居然是……-搜狐吃喝!!!
- Redis快速入门
- Html与CSS快速入门02-HTML基础应用
- net.sf.json.JSONException: There is a cycle in the hierarchy!的解决办法
- jQuery.on() 函数详解 【转载】
- Yii 事件
- #NOIP 2014# day.1 T3 飞扬的小鸟 bird
- TCP中URG和PSH的含义及区别
- python下的socket网络编程
- codeblocks网络编程不识别socket等
- 检讨——2016年7月26日11:06:13
- html5--导航栏制作
- android studio 快捷键
- get、post、ajax、getJSON四种请求方式
- 细说 CA 和证书
- java之数据库之Mysql及navicat的基本知识
- css3中word-wrap的用法?