View中inflate方法

 View headView = View.inflate(this,R.layout.item2,null);
 View headView = LayoutInflater.from(this).inflate(R.layout.item_head,listView1,false);

大家都知道如果用第二种方法填充条目,子 item 可以自己决定自己自身大小,但是第二种方法想让子 item 就不能决定自身的大小了。如果想要
在父布局中展现子 item 的大小,就要在子 item 外面套一层布局。如想要把一张图片()当作 listview 的头部:

第一种布局:
           android:id="@+id/iv"
           android:layout_width="match_parent"
           android:layout_height="160dp"
           android:orientation="vertical"
           android:src="@mipmap/ic_launcher_round"
    >
第二种布局:

    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
            android:layout_width="match_parent"
        android:layout_height="160dp"
        android:id="@+id/iv"
        android:src="@mipmap/ic_launcher_round"
        />
第二种布局 ImageView 就可以展示自己原有的大小,第一种布局,在父布局中的宽高是 wrap_content。

你可能感兴趣的:(View中inflate方法)