Android как заполнить родительский элемент, но оставить немного места

#android #android-layout #android-xml

#Android #android-макет #android-xml

Вопрос:

Название не очень хорошее, но я не знал, как еще описать

У меня есть a relative layout с 2 элементами, некоторым контейнером и кнопкой внизу.

Я хочу, чтобы контейнер заполнил все пространство в relativelayout, оставив достаточно места для размещения кнопки

вот пример:

 <?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>
 

вот как я хочу, чтобы это выглядело

Красный означает относительный макет, зеленый — линейный макет, а желтый — кнопку…

введите описание изображения здесь

как я могу это сделать?

Ответ №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>
 

Ответ №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"
    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>
 

Ответ №3:

Это должно дать вам что-то близкое к тому, что вы хотите:

 <?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>