自定义ViewGroup-通过内边距加深理解
2017-03-23 15:51
399 查看
读取 4 个方向的
padding:
public int getPaddingLeft () 离左边的 padding
public int getPaddingRight () 离右边的 padding
public int getPaddingTop () 离顶部的 padding
public int getPaddingRight () 离底部的 padding
CustomViewGroup3:
public class CustomViewGroup3 extends ViewGroup {
public CustomViewGroup3(Context context) {
super(context);
}
public CustomViewGroup3(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomViewGroup3(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//测量所有的子View
measureChildren(widthMeasureSpec, heightMeasureSpec);
//测量自己的宽高
int width = measureWidth(widthMeasureSpec);
int height = measureHeight(heightMeasureSpec);
setMeasuredDimension(width, height);
}
private int measureHeight(int widthMeasureSpec) {
//父控件建议自己测量的值
int measureMode = MeasureSpec.getMode(widthMeasureSpec);
int measureSize = MeasureSpec.getSize(widthMeasureSpec);
int width = 0;
if (measureMode == MeasureSpec.EXACTLY) {
width = measureSize;
} else if (measureMode == MeasureSpec.AT_MOST) {
int aWidth = 0;
int bWidth = 0;
int cWidth = 0;
int dWidth = 0;
for (int i = 0; i < getChildCount(); i++) {
if (i == 0) {
aWidth = getChildAt(i).getMeasuredWidth();//左边View的宽度
} else if (i == 1) {
bWidth = getChildAt(i).getMeasuredWidth();
} else if (i == 2) {
cWidth = getChildAt(i).getMeasuredWidth();
} else if (i == 3) {
dWidth = getChildAt(i).getMeasuredWidth();
}
}
//getPaddingTop:top离top的内边距 getPaddingBottom:bottom离底的内边距
width = Math.max(aWidth, bWidth) + Math.max(cWidth, dWidth)
+ getPaddingTop() + getPaddingBottom();//取到宽度的最大值
}
return width;
}
private int measureWidth(int heightMeasureSpec) {
int measureMode = MeasureSpec.getMode(heightMeasureSpec);
int measureSize = MeasureSpec.getSize(heightMeasureSpec);
int height = 0;
if (measureMode == MeasureSpec.EXACTLY) {
height = measureSize;
} else if (measureMode == MeasureSpec.AT_MOST) {
int aHeight = 0;
int bHeight = 0;
int cHeight = 0;
int dHeight = 0;
for (int i = 0; i < getChildCount(); i++) {
if (i == 0) {
aHeight = getChildAt(i).getMeasuredHeight();
} else if (i == 1) {
bHeight = getChildAt(i).getMeasuredHeight();
} else if (i == 2) {
cHeight = getChildAt(i).getMeasuredHeight();
} else if (i == 3) {
dHeight = getChildAt(i).getMeasuredHeight();
}
}
height = Math.max(aHeight, bHeight) + Math.max(cHeight, dHeight)
+ getPaddingLeft() + getPaddingRight();//取到宽度的最大值
}
return height;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int leftPadding = getPaddingLeft();
int rightPadding = getPaddingRight();
int topPadding = getPaddingTop();
int bottomPadding = getPaddingBottom();
for (int i = 0; i < getChildCount(); i++) {
View childView = getChildAt(i);//某一个子View
if (i == 0) {
childView.layout(leftPadding,
topPadding,
childView.getMeasuredWidth() + leftPadding,
childView.getMeasuredHeight() + topPadding);
} else if (i == 1) {
childView.layout(getMeasuredWidth() - childView.getMeasuredWidth() - rightPadding,
topPadding,
getMeasuredWidth() - rightPadding,
childView.getMeasuredHeight() + topPadding);
} else if (i == 2) {
childView.layout(leftPadding,
getMeasuredHeight() - childView.getMeasuredHeight() - bottomPadding,
childView.getMeasuredWidth() + leftPadding,
getMeasuredHeight() - bottomPadding);
} else if (i == 3) {
childView.layout(getMeasuredWidth() - childView.getMeasuredWidth() - rightPadding,
getMeasuredHeight() - childView.getMeasuredHeight() - bottomPadding,
getMeasuredWidth() - rightPadding,
getMeasuredHeight() - bottomPadding);
}
}
}
}
activity_main:
<com.example.user.myapplication4.CustomViewGroup3
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFCCCCCC"
android:paddingBottom="15dp"
android:paddingLeft="20dp"
android:paddingRight="30dp"
android:paddingTop="10dp">
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_blue_bright" />
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_blue_dark" />
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_red_dark" />
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_green_light" />
</com.example.user.myapplication4.CustomViewGroup3>
结果展示:
padding:
public int getPaddingLeft () 离左边的 padding
public int getPaddingRight () 离右边的 padding
public int getPaddingTop () 离顶部的 padding
public int getPaddingRight () 离底部的 padding
CustomViewGroup3:
public class CustomViewGroup3 extends ViewGroup {
public CustomViewGroup3(Context context) {
super(context);
}
public CustomViewGroup3(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomViewGroup3(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//测量所有的子View
measureChildren(widthMeasureSpec, heightMeasureSpec);
//测量自己的宽高
int width = measureWidth(widthMeasureSpec);
int height = measureHeight(heightMeasureSpec);
setMeasuredDimension(width, height);
}
private int measureHeight(int widthMeasureSpec) {
//父控件建议自己测量的值
int measureMode = MeasureSpec.getMode(widthMeasureSpec);
int measureSize = MeasureSpec.getSize(widthMeasureSpec);
int width = 0;
if (measureMode == MeasureSpec.EXACTLY) {
width = measureSize;
} else if (measureMode == MeasureSpec.AT_MOST) {
int aWidth = 0;
int bWidth = 0;
int cWidth = 0;
int dWidth = 0;
for (int i = 0; i < getChildCount(); i++) {
if (i == 0) {
aWidth = getChildAt(i).getMeasuredWidth();//左边View的宽度
} else if (i == 1) {
bWidth = getChildAt(i).getMeasuredWidth();
} else if (i == 2) {
cWidth = getChildAt(i).getMeasuredWidth();
} else if (i == 3) {
dWidth = getChildAt(i).getMeasuredWidth();
}
}
//getPaddingTop:top离top的内边距 getPaddingBottom:bottom离底的内边距
width = Math.max(aWidth, bWidth) + Math.max(cWidth, dWidth)
+ getPaddingTop() + getPaddingBottom();//取到宽度的最大值
}
return width;
}
private int measureWidth(int heightMeasureSpec) {
int measureMode = MeasureSpec.getMode(heightMeasureSpec);
int measureSize = MeasureSpec.getSize(heightMeasureSpec);
int height = 0;
if (measureMode == MeasureSpec.EXACTLY) {
height = measureSize;
} else if (measureMode == MeasureSpec.AT_MOST) {
int aHeight = 0;
int bHeight = 0;
int cHeight = 0;
int dHeight = 0;
for (int i = 0; i < getChildCount(); i++) {
if (i == 0) {
aHeight = getChildAt(i).getMeasuredHeight();
} else if (i == 1) {
bHeight = getChildAt(i).getMeasuredHeight();
} else if (i == 2) {
cHeight = getChildAt(i).getMeasuredHeight();
} else if (i == 3) {
dHeight = getChildAt(i).getMeasuredHeight();
}
}
height = Math.max(aHeight, bHeight) + Math.max(cHeight, dHeight)
+ getPaddingLeft() + getPaddingRight();//取到宽度的最大值
}
return height;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int leftPadding = getPaddingLeft();
int rightPadding = getPaddingRight();
int topPadding = getPaddingTop();
int bottomPadding = getPaddingBottom();
for (int i = 0; i < getChildCount(); i++) {
View childView = getChildAt(i);//某一个子View
if (i == 0) {
childView.layout(leftPadding,
topPadding,
childView.getMeasuredWidth() + leftPadding,
childView.getMeasuredHeight() + topPadding);
} else if (i == 1) {
childView.layout(getMeasuredWidth() - childView.getMeasuredWidth() - rightPadding,
topPadding,
getMeasuredWidth() - rightPadding,
childView.getMeasuredHeight() + topPadding);
} else if (i == 2) {
childView.layout(leftPadding,
getMeasuredHeight() - childView.getMeasuredHeight() - bottomPadding,
childView.getMeasuredWidth() + leftPadding,
getMeasuredHeight() - bottomPadding);
} else if (i == 3) {
childView.layout(getMeasuredWidth() - childView.getMeasuredWidth() - rightPadding,
getMeasuredHeight() - childView.getMeasuredHeight() - bottomPadding,
getMeasuredWidth() - rightPadding,
getMeasuredHeight() - bottomPadding);
}
}
}
}
activity_main:
<com.example.user.myapplication4.CustomViewGroup3
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFCCCCCC"
android:paddingBottom="15dp"
android:paddingLeft="20dp"
android:paddingRight="30dp"
android:paddingTop="10dp">
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_blue_bright" />
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_blue_dark" />
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_red_dark" />
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_green_light" />
</com.example.user.myapplication4.CustomViewGroup3>
结果展示:
相关文章推荐
- 自定义组件<六>:深入理解ViewGroup
- 自定义View(通过ViewGroup实现,Textview长度根据字数的多少而定的)
- 自定义ViewGroup来理解View的scrollTo()方法
- 通过自定义ViewGroup来实现侧滑菜单效果,解决滑动冲突
- Android学习自定义View(五)——自定义ViewGroup及其onMeasure()的理解
- 系统间通信:通过自定义协议通信加深HTTP理解
- 自定义ViewGroup基础巩固1---理解onLayout()方法
- 2014-11-8Android学习------深入理解Android中ViewGroup-----自定义组件
- 自定义ViewGroup---理解1
- Android学习自定义View(五)——自定义ViewGroup及其onMeasure()的理解
- 使用ViewGroup自定义数字键盘
- android之自定义ViewGroup和自动换行的布局的实现
- android之自定义ViewGroup和自动换行的布局的实现
- 继承ViewGroup的自定义类
- Android 自定义AlertDialog并通过AlertDialog修改TextView的值
- android 自定义ViewGroup和对view进行切图动画实现滑动菜单SlidingMenu
- android之自定义ViewGroup
- android ListView布局之三(使用自定义的Adapter绑定数据,通过contextView.setTag绑定数据)有按钮的ListView
- android 自定义view中onMeasure()理解
- android之自定义ViewGroup和自动换行的布局的实现