android自定义View之margin,padding,align

> 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);  
imageView.setPadding(left,top,right,bottom); 
// 动态设置margin,拿LinearLayout里边放ImageVIew例
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20, 20);  
params.setMargins(20, 0, 20, 0);  
imageView.setLayoutParams(params);  

-- android自定义View之margin和padding的处理- https://blog.csdn.net/u012732170/article/details/55045472
 1.padding  在自定义View中处理padding,只需要在onDraw()中处理,别忘记处理布局为wrap_content的情况。
 在自定义ViewGroup中处理padding,只需要在onLayout()中,给子View布局时算上padding的值即可,也别忘记处理布局为wrap_content的情况。
 2.margin  自定义View无需处理margin,在自定义ViewGroup中处理margin时,需要在onMeasure()中根据margin计算ViewGroup的宽、高,同时在onLayout中布局子View时也别忘记根据margin来布局。

-- Android 动态设置padding跟margin的问题- https://blog.csdn.net/qq_30552993/article/details/72898227
Android动态设置控件大小以及设定margin以及padding值- https://blog.csdn.net/tiramisu_ljh/article/details/62883349
 1.view.setPadding(int left, int top, int right, int bottom);
 2.LayoutParams lp = (LayoutParams) view.getLayoutParams();
   lp.setMargins(int left, int top, int right, int bottom

-- 自定义控件(13)-View绘制的Padding、Margin- https://blog.csdn.net/u013210620/article/details/49798345
        @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//CustomView extends View
        // 声明一个临时变量来存储计算出的测量值
        int resultWidth = 0;
 
        // 获取宽度测量规格中的mode
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
 
        // 获取宽度测量规格中的size
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        /*
         * 如果爹心里有数
         */
        if (modeWidth == MeasureSpec.EXACTLY) {
            // 那么儿子也不要让爹难做就取爹给的大小吧
            resultWidth = mBitmap.getWidth() + getPaddingLeft() + getPaddingRight();
        }
        /*
         * 如果爹心里没数
         */
        else {
            // 那么儿子可要自己看看自己需要多大了
            resultWidth = mBitmap.getWidth() + getPaddingLeft() + getPaddingRight();
 
            /*
             * 如果爹给儿子的是一个限制值
             */
            if (modeWidth == MeasureSpec.AT_MOST) {
                // 那么儿子自己的需求就要跟爹的限制比比看谁小要谁
                resultWidth = Math.min(resultWidth, sizeWidth);
            }
        }
 
        int resultHeight = 0;
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
 
        if (modeHeight == MeasureSpec.EXACTLY) {
            resultHeight = sizeHeight;
        } else {
            resultHeight = mBitmap.getHeight() + getPaddingTop() + getPaddingBottom();
            if (modeHeight == MeasureSpec.AT_MOST) {
                resultHeight = mBitmap.getHeight() + getPaddingTop() + getPaddingBottom(); 
            }
        }
 
        // 设置测量尺寸
        setMeasuredDimension(resultWidth, resultHeight);
    }

 //自定义View-设置padding没有作用的原因及解决
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(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  = Math.min(width,height)/2;
    canvas.drawCircle(paddingLeft+width/2,paddingTop+height/2,radius,paint);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值