View中inflate方法

来源:互联网 发布:大阪 知乎 编辑:程序博客网 时间:2024/06/03 19:58
 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 的头部:
<?xml version="1.0" encoding="utf-8"?>
第一种布局:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
           android:id="@+id/iv"
           android:layout_width="match_parent"
           android:layout_height="160dp"
           android:orientation="vertical"
           android:src="@mipmap/ic_launcher_round"
    >
</ImageView>
第二种布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="160dp"
        android:id="@+id/iv"
        android:src="@mipmap/ic_launcher_round"
        />
</LinearLayout>
第二种布局 ImageView 就可以展示自己原有的大小,第一种布局,在父布局中的宽高是 wrap_content。
原创粉丝点击