Android如何填充父母但留出一些空间

时间:2019-04-19 19:50:28

标签: android android-layout android-xml

标题不好,但我不知道该怎么形容

我有一个relative layout,其中包含2个元素,一个容器和一个底部的按钮。

我希望容器填充相对布局中的所有空间,只保留足以放置按钮的空间

这是一个示例:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout  //i'm using linearlayout as an example, it could be anything here
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/b3"
        android:gravity="center"
        android:orientation="vertical">


    </LinearLayout>

    <Button
        android:id="@+id/b3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="asdasdasdasdasdasdasdasd" />
</RelativeLayout>

这是我想要的样子

红色表示相对布局,绿色表示线性布局,黄色表示按钮...

enter image description here

我怎么做?

3 个答案:

答案 0 :(得分:1)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/red"
    android:padding="5dp"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/b"
        android:background="@color/green">

    </LinearLayout>
    <Button
        android:id="@+id/b"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="this is the button"
        android:background="@color/yellow"/>

</RelativeLayout>

答案 1 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignTop="@+id/b3"
        android:layout_margin="4dp"
        android:background="@color/colorPrimary"
        android:gravity="center"
        android:orientation="vertical">

    </LinearLayout>

    <Button
        android:id="@+id/b3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="asdasdasdasdasdasdasdasd" />
</RelativeLayout>

答案 2 :(得分:0)

这应该给您一些接近您想要的东西:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout  //i'm using linearlayout as an example, it could be anything here
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="3"
    android:gravity="center"
    android:orientation="vertical">
</LinearLayout>

<Button
    android:id="@+id/b3"
    android:layout_width="wrap_content"
    android:layout_height="0px"
    android:layout_weight="1" 
    android:text="asdasdasdasdasdasdasdasd" />

</LinearLayout>