【问题标题】:Android: How to align Linear Layout inside FrameLayout to bottom?Android:如何将 FrameLayout 内的线性布局对齐到底部?
【发布时间】:2016-08-04 11:04:47
【问题描述】:

我的布局结构如下:

我需要将第二个 LinearLayout 与底部对齐,这样如果第二个 LinearLayout 内部视图上的任何部分变得可见,则应调整第二个布局的大小以使其仍然可见。

请问我该如何以正确的方式做到这一点?

非常感谢您的建议。

【问题讨论】:

  • 这里有必要用FrameLayout吗???
  • 我认为是的,因为底部布局应该在第一个之上,因为可以在第一个LinearLayout之上展开而不缩小屏幕。
  • 但这也是您可以使用RelativeLayout 实现的目标。

标签: android android-layout android-linearlayout android-framelayout


【解决方案1】:

使用属性 layout_gravity 对齐 FrameLayout 内的任何视图

<LinearLayout ...
android:layout_gravity:"bottom"
/>

但是如果你想把它放在其他布局的底部。然后改用相对布局。

例子

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="52dp"
        android:layout_alignParentTop="true"
        android:background="@android:color/holo_red_dark"
        android:gravity="center"
        android:text="some text view"
        android:textColor="#fff"
        android:textStyle="bold"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/text"
        android:background="#123123"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="52dp"
            android:layout_alignParentTop="true"
            android:background="@android:color/darker_gray"
            android:gravity="center"
            android:text="some text view at top inside "
            android:textColor="#fff"
            android:textStyle="bold"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="52dp"
            android:layout_alignParentTop="true"
            android:background="@android:color/holo_blue_bright"
            android:gravity="center"
            android:text="some text view at second position "
            android:textColor="#fff"
            android:textStyle="bold"/>
    </LinearLayout>

</RelativeLayout>

对于您的问题“第二个布局应调整大小以使其仍然可见。”

最好使用RelativeLayout。 假设您的第二个 LinearLayout 位于 RelativeLayout 的底部。 如果它没有子视图,如果它的高度设置为wrap_content,它将是不可见的。每当您向其添加一些视图时。它会自行调整大小。

【讨论】:

    【解决方案2】:

    使用重力属性,如

    <LinearLayout
      android:width="match_parent"
     android:height="match_parent"
     android:layout_gravity:"bottom">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-05
      • 1970-01-01
      • 2021-10-07
      • 2013-11-05
      • 1970-01-01
      • 2014-01-02
      • 2018-03-27
      • 1970-01-01
      相关资源
      最近更新 更多