Здесь мой код макета;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="@string/welcome"
android:id="@+id/TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
<LinearLayout android:id="@+id/LinearLayout"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom">
<EditText android:id="@+id/EditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>
<Button android:text="@string/label_submit_button"
android:id="@+id/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
</LinearLayout>
Похоже, что это слева, и я хочу, чтобы он выглядел справа.
Очевидным ответом является установка TextView на fill_parent по высоте, но это не оставляет места для кнопки или поля ввода. По сути, проблема заключается в том, что я хочу, чтобы кнопка отправки и текстовая запись были фиксированной высотой внизу и текстовым видом, чтобы заполнить остальную часть пространства, аналогично горизонтальной линейной компоновке. Я хочу, чтобы кнопка отправки обернула ее содержимое и для ввода текста, чтобы заполнить остальную часть пространства.
Если первый элемент в линейной компоновке сказал fill_parent, он делает именно это, не оставляя места для других предметов, как мне получить элемент, который первым в линейном макете, чтобы заполнить все пространство, кроме минимума, необходимого для остальные элементы в макете?
EDIT:
Относительные макеты действительно были ответом - Спасибо!
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:text="@string/welcome"
android:id="@+id/TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
</TextView>
<RelativeLayout
android:id="@+id/InnerRelativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<Button
android:text="@string/label_submit_button"
android:id="@+id/Button"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<EditText
android:id="@+id/EditText"
android:layout_width="fill_parent"
android:layout_toLeftOf="@id/Button"
android:layout_height="wrap_content">
</EditText>
</RelativeLayout>
</RelativeLayout>
Я думаю, вы должны попробовать относительный макет.
Если у вас есть относительный макет, заполняющий весь экран, вы можете использовать android:layout_alignParentBottom
, чтобы переместить кнопку в нижней части экрана.
Если ваши представления внизу не показаны в относительном макете, то, возможно, макет над ним занимает все пространство. В этом случае вы можете поместить представление, которое должно быть внизу, сначала в вашем файле макета, и расположите остальную часть макета над представлениями с помощью android:layout_above
. Это позволяет нижнему виду занять столько места, сколько потребуется, а остальная часть макета может заполнить всю оставшуюся часть экрана.
В ScrollView
это не работает, так как RelativeLayout
будет перекрывать все, что находится в ScrollView
в нижней части страницы.
Я исправил его с помощью динамического растяжения FrameLayout
:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<!-- content goes here -->
<!-- stretching frame layout, using layout_weight -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
<!-- content fixated to the bottom of the screen -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- your bottom content -->
</LinearLayout>
</LinearLayout>
</ScrollView>
Вы можете сохранить свой первоначальный линейный макет, вставив относительный макет в линейный макет:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="welcome"
android:id="@+id/TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:text="submit"
android:id="@+id/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true">
</Button>
<EditText android:id="@+id/EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/Button"
android:layout_alignParentBottom="true">
</EditText>
</RelativeLayout>
</LinearLayout>
Ответ выше (по Janusz) вполне корректен, но я лично не чувствую себя на 100% совместимым с RelativeLayouts, поэтому предпочитаю вводить "filler", пустой TextView, например:
<!-- filler -->
<TextView android:layout_height="0dip"
android:layout_width="fill_parent"
android:layout_weight="1" />
перед элементом, который должен находиться в нижней части экрана.
Это также работает.
<LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_below="@+id/linearLayout3"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
android:gravity="bottom"
android:layout_alignParentBottom="true"
android:layout_marginTop="20dp"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
/>
</LinearLayout>
Вы можете сделать это с помощью LinearLayout или ScrollView. Иногда легче реализовать, чем RelativeLayout. Единственное, что вам нужно сделать, это добавить следующий вид до Представления, которые вы хотите выровнять в нижней части экрана:
<View
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />
Это создает пустой вид, заполняя пустое пространство и нажимая следующие представления в нижней части экрана.
Следуя элегантному решению Timores, я обнаружил, что следующее создает вертикальное заполнение вертикального LinearLayout и горизонтальное заполнение горизонтального LinearLayout:
<Space
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
Вам даже не нужно вставлять второй макет relative
внутри первого. Просто используйте android:layout_alignParentBottom="true"
в Button и EditText.
Если вы не хотите делать много изменений, вы можете просто поставить:
android:layout_weight="1"
для TextView с ID как @+id/TextView
i.e
<TextView android:text="@string/welcome"
android:id="@+id/TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</TextView>
Создавая как HEADER, так и FOOTER, вот пример
[Layout XML]
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/backgroundcolor"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#FF0000">
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:background="#FFFF00">
</RelativeLayout>
</RelativeLayout>
[Снимок экрана]
Надеюсь, это полезно. Спасибо!!
используйте нижнюю кнопку выравнивания кода, чтобы уменьшить ее рабочие
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_back"
android:layout_width="100dp"
android:layout_height="80dp"
android:text="Back" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.97"
android:gravity="center"
android:text="Payment Page" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"/>
</LinearLayout>
</LinearLayout>
Если у вас есть такая иерархия:
<ScrollView>
|-- <RelativeLayout>
|-- <LinearLayout>
Сначала примените android:fillViewport="true"
к ScrollView
, а затем примените android:layout_alignParentBottom="true"
к LinearLayout
.
Это сработало для меня отлично.
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:scrollbars="none"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/linearLayoutHorizontal"
android:layout_alignParentBottom="true">
</LinearLayout>
</RelativeLayout>
</ScrollView>
Используйте android:layout_alignParentBottom="true"
в <RelativeLayout>
.
Это определенно поможет.
ConstraintLayout
в корневом макетеИ установите app:layout_constraintBottom_toBottomOf="parent"
, чтобы Layout в нижней части экрана
<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent">
</LinearLayout>
FrameLayout
в корневом макетеПросто установите android:layout_gravity="bottom"
в вашем макете
<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal">
</LinearLayout>
LinearLayout
в корневом макете (android:orientation="vertical"
) (1) Установите макет android:layout_weight="1"
в верхней части макета
<TextView
android:id="@+id/TextView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="welcome" />
(2) Установите дочерний элемент LinearLayout
для android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="bottom"
Основной атрибут ndroid:gravity="bottom"
, пусть дочерний вид в нижней части макета.
<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="horizontal">
</LinearLayout>
RelativeLayout
в корневом макетеИ установите android:layout_alignParentBottom="true"
, чтобы макет в нижней части экрана
<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
</LinearLayout>
Вы можете просто дать вашему верхнему дочернему виду (TextView @+ id/TextView) атрибут:
android:layout_weight="1"
.
Это приведет к тому, что все остальные элементы будут внизу внизу.
Для такого случая всегда используйте RelativeLayouts. LinearLayout не предназначен для такого использования.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/db1_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Place your layout here -->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:paddingLeft="20dp"
android:paddingRight="20dp" >
<Button
android:id="@+id/setup_macroSavebtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Save" />
<Button
android:id="@+id/setup_macroCancelbtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
</LinearLayout>
</RelativeLayout>
Это можно сделать и с линейной компоновкой Просто поставьте Height = 0dp и weight = 1 в макет выше, а тот, который вы хотите в нижней части, просто напишите height = wrap content и no weight. То, что он делает, это предоставление содержимого обложки для макета (тот, который содержит текст редактирования и кнопку), а затем тот, который имеет вес, занимает остальную часть макета. Я обнаружил это случайно.
Я использовал решение Janusz, но добавил добавление к последнему представлению, так как верхняя часть моего макета была ScrollView. ScrollView будет частично скрыт, поскольку он растет с контентом. Использование android: paddingBottom в последнем представлении помогает показать все содержимое в ScrollView.