Android LinearLayout排列方式为vertical时 让内部组件位于最底部

背景:当LinearLayout设定android:orientation="vertical"后,只有水平方向的gravity属性才生效,比如left、right、center_horizontal。此时若只在最后一个组件中添加android:gravity="bottom"或者设定layout_marginBottom属性值是无效的。

所以,此时想要让最后一个控件置于父布局底部,可将某个可操作的、不需置底的控件,添加android:layout_weight="1"属性。

例:(删掉了一些与这个例子无关的非必要代码)

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
      android:layout_width="match_parent"
      android:layout_height="68dp"
      android:layout_marginTop="40dp"
      android:gravity="center_vertical"
      android:text="@string/read_head"/>
    <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginTop="22dp"
      android:layout_weight="1"					//这里
      android:text="@string/read_detail"/>
    
    <ImageView
      android:layout_width="120dp"
      android:layout_height="120dp"
      android:layout_marginBottom="74dp"
      android:src="@drawable/reading"/>
  </LinearLayout>

当没添加这行代码时,显示:
在这里插入图片描述
添加后:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_40934065/article/details/117930317