> margin,padding,align
align,与指定的组件某位置的边缘进行对齐;
margin就是指子控件与外部控件的距离,俗称外边距;
padding就是指控件内容与控件边界的距离,俗称内边距。
android:layout_marginxxx的用法是指当前组件距离其父组件在xxx方向上的边距
一个LinearLayout中有一个ImageView,设置ImageView的padding和margin的属性,那么padding指的是ImageView中图片距离图片框的距离; margin指的是整一个ImageView距离LinearLayout边界的距离。
// 动态设置padding,拿ImageView为例
ImageView imageView = new ImageView(Context context);
(left,top,right,bottom);
// 动态设置margin,拿LinearLayout里边放ImageVIew例
params = new (20, 20);
(20, 0, 20, 0);
(params);
-- android自定义View之margin和padding的处理- /u012732170/article/details/55045472
在自定义View中处理padding,只需要在onDraw()中处理,别忘记处理布局为wrap_content的情况。
在自定义ViewGroup中处理padding,只需要在onLayout()中,给子View布局时算上padding的值即可,也别忘记处理布局为wrap_content的情况。
自定义View无需处理margin,在自定义ViewGroup中处理margin时,需要在onMeasure()中根据margin计算ViewGroup的宽、高,同时在onLayout中布局子View时也别忘记根据margin来布局。
-- Android 动态设置padding跟margin的问题- /qq_30552993/article/details/72898227
Android动态设置控件大小以及设定margin以及padding值- /tiramisu_ljh/article/details/62883349
(int left, int top, int right, int bottom);
lp = (LayoutParams) ();
(int left, int top, int right, int bottom
-- 自定义控件(13)-View绘制的Padding、Margin- /u013210620/article/details/49798345
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//CustomView extends View
// 声明一个临时变量来存储计算出的测量值
int resultWidth = 0;
// 获取宽度测量规格中的mode
int modeWidth = (widthMeasureSpec);
// 获取宽度测量规格中的size
int sizeWidth = (widthMeasureSpec);
/*
* 如果爹心里有数
*/
if (modeWidth == ) {
// 那么儿子也不要让爹难做就取爹给的大小吧
resultWidth = () + getPaddingLeft() + getPaddingRight();
}
/*
* 如果爹心里没数
*/
else {
// 那么儿子可要自己看看自己需要多大了
resultWidth = () + getPaddingLeft() + getPaddingRight();
/*
* 如果爹给儿子的是一个限制值
*/
if (modeWidth == MeasureSpec.AT_MOST) {
// 那么儿子自己的需求就要跟爹的限制比比看谁小要谁
resultWidth = (resultWidth, sizeWidth);
}
}
int resultHeight = 0;
int modeHeight = (heightMeasureSpec);
int sizeHeight = (heightMeasureSpec);
if (modeHeight == ) {
resultHeight = sizeHeight;
} else {
resultHeight = () + getPaddingTop() + getPaddingBottom();
if (modeHeight == MeasureSpec.AT_MOST) {
resultHeight = () + getPaddingTop() + getPaddingBottom();
}
}
// 设置测量尺寸
setMeasuredDimension(resultWidth, resultHeight);
}
//自定义View-设置padding没有作用的原因及解决
@Override
protected void onDraw(Canvas canvas) {
(canvas);
int paddingLeft = getPaddingLeft();
int paddingRight = getPaddingRight();
int paddingTop = getPaddingTop();
int paddingBottom = getPaddingBottom();
int width = getWidth()-paddingLeft-paddingRight;
int height = getHeight()-paddingBottom-paddingTop;
int radius = (width,height)/2;
(paddingLeft+width/2,paddingTop+height/2,radius,paint);
}