美文网首页
LayoutInflater学习笔记

LayoutInflater学习笔记

作者: JeffMei | 来源:发表于2017-07-27 19:05 被阅读0次
    • LayoutInflater 内部是用Android提供的pull解析方式来解析布局文件的

    • 根据节点,用反射的方式创建出View的实例并返回

    • 下面是LayoutInflater加载View大概的函数调用图:

    直接LayoutInflater.inflate()加进来的Button不能修改大小

    这是因为 layout_widthlayout_height 是用于设置View在布局中的大小的,也就是说首先View必须存在于一个布局中,像这里的Button是没有父布局的,所以它的 layout_widthlayout_height 属性就失效了

    <Button
        android:id="@+id/btn_layout_inflater"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="50dp"
        android:text="LayoutInflater"
        xmlns:android="http://schemas.android.com/apk/res/android" />
    

    在外层加一个FrameLayout,layout_widthlayout_height 属性就可以生效了

    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:id="@+id/btn_layout_inflater"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="50dp"
            android:text="LayoutInflater"/>
    
    </FrameLayout>
    
    

    那么我们平时的xml布局中,最外层的viewGroup为什么又可以设置大小呢?

    那是因为Android会自动在布局文件的最外层再嵌套一个FrameLayout,所以才能生效

    相关文章

      网友评论

          本文标题:LayoutInflater学习笔记

          本文链接:https://www.haomeiwen.com/subject/uvuzkxtx.html