Listview佈局小技巧

    在開發過程中,我們經常會遇到這樣一種需求:listview下面有一個按鈕或者其他佈局,在listview內容較少時,能夠緊貼在listview下方;當listview內容超過屏幕顯示時,又能夠貼在屏幕底部。這裏有一個簡單的佈局方式,可以實現需求。代碼如下:
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ListView
            android:id="@+id/charge_list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
        
        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center"
            android:background="@android:color/holo_red_dark"
            android:text="按鈕" />

</LinearLayout>
    關鍵就在於父控件的高度要設置成wrap_content,這樣內容較少時textview可以緊貼listview下面;內容較多是相當於match_parent。listview要設置weight爲1,這樣在內容較多時就可以實現textview貼在屏幕底部了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章