在开发过程中,我们经常会遇到这样一种需求: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贴在屏幕底部了。