Android 如何置底一个View

本文探讨了如何使用XML布局文件实现ListView位于Buttons下方,Buttons位于屏幕底部的效果,通过三种方法来解决布局顺序问题,并提供了相关代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要求listview位于一系列buttons的上方,buttons在屏幕底部

用relativelayout

法一:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
 
    <!-- Button 置底 --> 
    <LinearLayout android:id="@+id/test_bottom_buttons" 
        android:layout_width="fill_parent" android:layout_height="wrap_content" 
        android:orientation="horizontal" android:layout_alignParentBottom="true"> 
 
        <Button android:layout_width="wrap_content" 
            android:layout_height="wrap_content" android:text="确定"></Button> 
 
        <Button android:layout_width="wrap_content" 
            android:layout_height="wrap_content" android:text="取消"></Button> 
 
    </LinearLayout> 
 
    <ListView android:id="@+id/lv_test" android:layout_width="fill_parent" 
        android:layout_height="fill_parent" android:layout_above="@id/test_bottom_buttons"> 
    </ListView> 
 
</RelativeLayout> 


但是,在xml中却是buttons在listview上方,不符合常理

编译时要求先找到id/test_bottom_buttons,只好这样写了,不便于阅读

法二:

试图把ID的声明提前

编写一个新的myids.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <!-- 手动声明资源ID,类似于C++中的前置声明 --> 
    <item type="id" name="test_bottom_buttons"></item> 
</resources> 

这样便手动向系统添加了一个ID  test_bottom_buttons   系统会根据这条xml代码在R类中生成对应的int型资源。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
 
  <ListView android:id="@+id/lv_test" android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:layout_above="@id/test_bottom_buttons"> 
  </ListView> 
   
    <!-- Button 置底 --> 
    <LinearLayout android:id="@id/test_bottom_buttons" 
        android:layout_width="fill_parent" android:layout_height="wrap_content" 
        android:orientation="horizontal" android:layout_alignParentBottom="true"> 
 
        <Button android:layout_width="wrap_content" 
            android:layout_height="wrap_content" android:text="确定"></Button> 
 
        <Button android:layout_width="wrap_content" 
            android:layout_height="wrap_content" android:text="取消"></Button> 
 
    </LinearLayout> 
 
</RelativeLayout> 
法三:

可以不换顺序
 List中定义:android:layout_above="@+id/test_bottom_buttons" 
 Button中定义:android:id="@id/test_bottom_buttons"
这样得到的是lisview在buttons上方

补充:如果没有写两者的相对位置

 <?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
 
    <ListView android:id="@+id/lv_test" android:layout_width="fill_parent" 
        android:layout_height="fill_parent"> 
    </ListView> 
    <!-- Button 置底 --> 
    <LinearLayout android:id="@+id/test_bottom_buttons" 
        android:layout_width="fill_parent" android:layout_height="wrap_content" 
        android:orientation="horizontal" android:layout_alignParentBottom="true"> 
 
        <Button android:layout_width="wrap_content" 
            android:layout_height="wrap_content" android:text="确定"></Button> 
 
        <Button android:layout_width="wrap_content" 
            android:layout_height="wrap_content" android:text="取消"></Button> 
    </LinearLayout> 
     
</RelativeLayout> 
那么


按钮悬浮在listview上面.

参考文档:点击打开链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值