Android布局中LinearLayout的android:layout_gravity="bottom"不起作用

本文探讨了在界面布局中使用LinearLayout的局限性,尤其是在尝试将控件放置于屏幕底部时遇到的问题。通过对比说明了RelativeLayout的优势,并提供了解决方案,即将LinearLayout嵌套在RelativeLayout中并使用alignParentBottom属性来实现底部对齐。

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

 在做界面布局的时候,我们经常用到的就是LinearLayout,但是有时候用起来也不方便,比如我们在页面中要把一个Button放在屏幕底部,此时我们设置android:layout_gravity="bottom"是没有效果的。为什么呢?对于
 LinearLayout:

当 android:orientation="vertical"  时, 只有水平方向的设置才起作用,垂直方向的设置不起作用。即:left,right,center_horizontal 是生效的。      当 android:orientation="horizontal" 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用。即:top,bottom,center_vertical 是生效的。   

     如果我们要实现在手机屏幕上把某一个View放在底部,可以在我们布局的xml的最外层(或根布局)用RelativeLayout。再在RelativeLayout中包含我们写好的LinearLayout,把要显示在手机屏幕底端的view要加上属性android:layout_alignParentBottom="true",这样就可以了。

``` <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/color_F2E7DB"> <FrameLayout android:id="@+id/fit_status_bar" android:layout_width="match_parent" android:layout_height="53dp" android:alpha="0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <LinearLayout android:id="@+id/ll_search" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" android:paddingHorizontal="12dp" android:paddingVertical="12dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <ImageView android:id="@+id/iv_back" android:layout_width="40dp" android:background="@drawable/clickable_white" android:layout_height="40dp" android:padding="8dp" android:scaleType="centerCrop" android:src="@drawable/icon_back" /> <com.stary.cateyes.widget.SearchEditText android:id="@+id/et_search" android:layout_width="0dp" android:layout_weight="1" android:layout_height="32dp" android:layout_marginEnd="15dp" android:layout_marginStart="@dimen/dp_4" /> <FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/open_window" android:background="@drawable/icon_window_size"> <TextView android:id="@+id/add" android:layout_width="18dp" android:layout_height="20dp" android:gravity="right|bottom" android:paddingRight="2dp" android:text="9" android:textSize="10sp" android:textColor="#000000" android:textStyle="bold" /> </FrameLayout> </LinearLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/rv_type" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/ll_search" /> <View android:id="@+id/view" android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginTop="4dp" android:background="@color/color_D7CCC1" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/rv_type" /> <FrameLayout android:id="@+id/fl_fragment" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/view" /> </androidx.constraintlayout.widget.ConstraintLayout> </layout>```必须要创建列表项布局文件item_suggestion.xml吗?
03-13
以下代码,请帮我将Textview修改为一个imageview 。谢谢 。<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:didi="http://schemas.android.com/apk/res-auto" > <com.blog.demo10.NestedViewGroup android:id="@+id/dd_view_group" android:layout_width="match_parent" android:layout_height="match_parent" didi:header_id="@+id/view_bg" didi:target_id="@+id/target_layout" didi:inn_id="@+id/inner_rv" didi:header_init_top="0" didi:target_init_bottom="250"> <TextView android:id="@+id/view_bg" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:textColor="#f00" android:textSize="25sp" android:textAllCaps="false" android:text="@string/txt_nested_scroll_bg" /> <LinearLayout android:id="@+id/target_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#fff"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/inner_rv" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> </com.blog.demo10.NestedViewGroup> <androidx.appcompat.widget.Toolbar android:id="@+id/my_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:elevation="4dp" android:theme="@style/ThemeOverlay.AppCompat.ActionBar" didi:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> </RelativeLayout>
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值