Android layout_Android源码

Android layout_Android源码LayoutParams源码分析LayoutParams是布局参数的意思,我们在XML布局文件里的layout_xxx等属性都是对LayoutParams的描述。LayoutParams不属于View,是ViewGroup控制View的具体显示在哪里。

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

LayoutParams源码分析

概述

  • LayoutParams是布局参数的意思,我们在XML布局文件里的layout_xxx等属性都是对LayoutParams的描述。
  • LayoutParams不属于View,是ViewGroup控制View的具体显示在哪里。

LayoutParams基本用法

TextView textView1 = new TextView(this);
textView1.setText("TextView1");
linearLayout.addView(textView1);

TextView textView2 = new TextView(this);
textView2.setText("TextView2");
textView2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.addView(textView2);

TextView textView3 = new TextView(this);
textView3.setText("TextView3");
textView3.setLayoutParams(new LinearLayout.LayoutParams(100, 100));
linearLayout.addView(textView3);

LayoutParams源码分析

LayoutParams继承关系

ViewGroup.LayoutParams

public LayoutParams(Context c, AttributeSet attrs) { 
   
    TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_Layout);
    setBaseAttributes(a,
                      R.styleable.ViewGroup_Layout_layout_width,
                      R.styleable.ViewGroup_Layout_layout_height);
    a.recycle();
}

public LayoutParams(int width, int height) { 
   
    this.width = width;
    this.height = height;
}

public LayoutParams(LayoutParams source) { 
   
    this.width = source.width;
    this.height = source.height;
}

LayoutParams() { 
   
}

ViewGroup.MarginLayoutParams

public static class MarginLayoutParams extends ViewGroup.LayoutParams { 
   
    public int leftMargin;

    public int topMargin;

    public int rightMargin;

    public int bottomMargin;

    private int startMargin = DEFAULT_MARGIN_RELATIVE;

    private int endMargin = DEFAULT_MARGIN_RELATIVE;

    public static final int DEFAULT_MARGIN_RELATIVE = Integer.MIN_VALUE;

    public MarginLayoutParams(Context c, AttributeSet attrs) { 
   
        super();

        TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_MarginLayout);
        setBaseAttributes(a,
                          R.styleable.ViewGroup_MarginLayout_layout_width,
                          R.styleable.ViewGroup_MarginLayout_layout_height);

        int margin = a.getDimensionPixelSize(
            com.android.internal.R.styleable.ViewGroup_MarginLayout_layout_margin, -1);
        if (margin >= 0) { 
   
            leftMargin = margin;
            topMargin = margin;
            rightMargin= margin;
            bottomMargin = margin;
        } else { 
   
            int horizontalMargin = a.getDimensionPixelSize(
                R.styleable.ViewGroup_MarginLayout_layout_marginHorizontal, -1);
            int verticalMargin = a.getDimensionPixelSize(
                R.styleable.ViewGroup_MarginLayout_layout_marginVertical, -1);
            ...
        }
        ...
    }
}

addView流程

public void addView(View child) { 
   
    addView(child, -1);
}

public void addView(View child, int index) { 
   
    if (child == null) { 
   
        throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
    }
    LayoutParams params = child.getLayoutParams();

    //若子View的LayoutParams为null,则使用默认LayoutParams
    if (params == null) { 
   
        params = generateDefaultLayoutParams();
        if (params == null) { 
   
            throw new IllegalArgumentException(
                "generateDefaultLayoutParams() cannot return null");
        }
    }
    addView(child, index, params);
}

//生成默认LayoutParams
@Override
protected LayoutParams generateDefaultLayoutParams() { 
   
    if (mOrientation == HORIZONTAL) { 
   
        return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    } else if (mOrientation == VERTICAL) { 
   
        return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    }
    return null;
}

public void addView(View child, int index, LayoutParams params) { 
   
    if (DBG) { 
   
        System.out.println(this + " addView");
    }

    if (child == null) { 
   
        throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
    }

    requestLayout();
    invalidate(true);
    addViewInner(child, index, params, false);
}

private void addViewInner(View child, int index, LayoutParams params,
                          boolean preventRequestLayout) { 
   
    ...
        //检查LayoutParams是否合法,若不合法则使用默认值
        if (!checkLayoutParams(params)) { 
   
            params = generateLayoutParams(params);
        }
    ...
        //子View添加到mChildren数组
        addInArray(child, index);
    ...
}

//检查LayoutParams是否合法
@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { 
   
    return p instanceof LinearLayout.LayoutParams;
}

@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) { 
   
    return new LinearLayout.LayoutParams(getContext(), attrs);
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/191001.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 反编译so库激活成功教程so

    反编译so库激活成功教程so所需工具1.IDA_Pro_v6.8_and_Hex-Rays_Decompiler_2.WinHex3.ARM_ASM背景:I2C通讯时报logCameraHal_Marvin:HAL-MOCKUP:HalReadI2CMem(1578):bus_num(7)orreg_addr_size(1)isinvalidateCameraHal_Marvin:…

    2022年6月16日
    37
  • springboot test 测试

    springboot test 测试最近项目中写了一个模块,只写到service给其他地方调用就行,不需要写接口这样的话测试就不方便了,于是想起了springboottest,这里有几个需要注意的地方就是test类必须和主启动类所在的包路径一样,不然报错,还有test类需要加入注解@SpringBootTest(classes=主启动类.class),@RunWith指定的运行器,我只用了springrunner,其他的还有junit,Suite等,然后在方法上加入@Test就可以运行了,运行的方式选择(run方法名withcove

    2022年5月4日
    63
  • vim忽略大写和小写查找配置

    vim忽略大写和小写查找配置

    2022年1月20日
    53
  • list,tensor,numpy相互转化

    list,tensor,numpy相互转化1.1list转numpyndarray=np.array(list)1.2numpy转listlist=ndarray.tolist()2.1list转torch.Tensortensor=torch.Tensor(list)2.2torch.Tensor转list先转numpy,后转listlist=tenso…

    2022年10月19日
    0
  • S3C2440移植uboot之支持NAND启动

    S3C2440移植uboot之支持NAND启动上一节S3C2440移植uboot之新建单板_时钟_SDRAM_串口移植uboot初始化了时钟,配置了支持串口,这一节我们继续修改uboot支持NAND启动。

    2022年6月13日
    36
  • 摄影后期人像高端摄影后期PS修图技巧[通俗易懂]

    摄影后期人像高端摄影后期PS修图技巧[通俗易懂]先自我介绍一下,叶子,职业修图师,从事数码后期行业12余载,擅长人像后期处理,婚纱照商业化修图,热爱摄影,喜欢旅行,总是用照片讲述故事。本文会从什么是**『皮肤质感』**,要修成这样的效果需要什么前置条件以及在过程中我们需要注意哪些核心要点为基准详细展开,意在让大家彻底明白怎样才能做出商业修图的皮肤效果。全文3504字,阅读时间约9分钟,如果觉得不愿意全看的话,可以直接拉到最后看结论。不…

    2022年6月15日
    34

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号