Android:如何将FrameLayout内的线性布局与底部对齐?

时间:2016-08-04 11:04:47

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

我有以下布局结构:

enter image description here

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

我怎么能以正确的方式做到这一点?

非常感谢您的任何建议。

2 个答案:

答案 0 :(得分: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>

result of above code

对于您的问题&#34;第二个布局应调整为仍然可见。&#34;

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

答案 1 :(得分:1)

使用重力属性,如

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

相关问题