Как сделать макет с View заполнить оставшееся пространство?
Я разрабатываю свой пользовательский интерфейс для приложений. Мне нужен макет, который выглядит так:
![Example of desired layout]()
(< и > являются кнопками). Проблема в том, что я не знаю, как убедиться, что TextView заполнит оставшееся пространство, причем две кнопки имеют фиксированный размер.
Если я использую fill_parent для текстового представления, вторая кнопка ( > ) не может быть показана.
Как я могу создать макет, который выглядит как изображение?
Ответы
Ответ 1
Ответ от woodshy работал у меня, и это проще, чем ответ Ungureanu Liviu, поскольку он не использует RelativeLayout
.
Я даю свой макет для ясности:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width = "80dp"
android:layout_weight = "0"
android:layout_height = "wrap_content"
android:text="<"/>
<TextView
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:layout_weight = "1"/>
<Button
android:layout_width = "80dp"
android:layout_weight = "0"
android:layout_height = "wrap_content"
android:text=">"/>
</LinearLayout>
Ответ 2
В случае, если < TEXT VIEW > находится в LinearLayout, задает свойство Layout_weight of < и > 0 и 1 для TextView.
В случае RelativeLayout align < и > влево и вправо и установите свойство "Макет слева" и "Макет справа" от TextView до идентификаторов < и >
Ответ 3
Если вы используете RelativeLayout
, вы можете сделать это примерно так:
<RelativeLayout
android:layout_width = "fill_parent"
android:layout_height = "fill_parent">
<ImageView
android:id = "@+id/my_image"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignParentTop ="true" />
<RelativeLayout
android:id="@+id/layout_bottom"
android:layout_width="fill_parent"
android:layout_height = "50dp"
android:layout_alignParentBottom = "true">
<Button
android:id = "@+id/but_left"
android:layout_width = "80dp"
android:layout_height = "wrap_content"
android:text="<"
android:layout_alignParentLeft = "true"/>
<TextView
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:layout_toLeftOf = "@+id/but_right"
android:layout_toRightOf = "@id/but_left" />
<Button
android:id = "@id/but_right"
android:layout_width = "80dp"
android:layout_height = "wrap_content"
android:text=">"
android:layout_alignParentRight = "true"/>
</RelativeLayout>
</RelativeLayout>
Ответ 4
Используя ConstraintLayout
, я нашел что-то вроде
<Button
android:id="@+id/left_button"
android:layout_width="80dp"
android:layout_height="48dp"
android:text="<"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/left_button"
app:layout_constraintRight_toLeftOf="@+id/right_button"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/right_button"
android:layout_width="80dp"
android:layout_height="48dp"
android:text=">"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
работает. Ключ соответствующим образом задает правильные, левые, верхние и нижние грани, затем устанавливая ширину и высоту на 0dp
и позволяя определить ее собственный размер.
Ответ 5
Это просто
Вы устанавливаете minWidth или minHeight, в зависимости от того, что вы ищете, горизонтального или вертикального.
И для другого объекта (того, который вы хотите заполнить в оставшемся пространстве) вы установите вес 1 (установите ширину, чтобы обернуть его содержимое), поэтому он заполнит остальную часть области.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center|left"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="80dp"
android:layout_height="fill_parent"
android:minWidth="80dp" >
</LinearLayout>
Ответ 6
вы можете использовать атрибут high layout_weight. Ниже вы можете увидеть макет, где ListView занимает все свободное пространство с кнопками внизу:
<LinearLayout 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"
tools:context=".ConfigurationActivity"
android:orientation="vertical"
>
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1000"
/>
<Button
android:id="@+id/btnCreateNewRule"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Create New Rule" />
<Button
android:id="@+id/btnConfigureOk"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Ok" />
</LinearLayout>
Ответ 7
Для тех, у кого такой же сбой с <LinearLayout...>
, как и я:
Важно указать android:layout_width="fill_parent"
, он не будет работать с wrap_content
.
OTOH, вы можете опустить android:layout_weight = "0"
, это не требуется.
Мой код в основном такой же, как и код fooobar.com/questions/45237/... (автор Vivek Pandey)
Ответ 8
Вам следует избегать вложенности 2 относительного макета, поскольку относительная компоновка всегда делает 2-х прохода для рисования (против 1 для любого другого типа макета). Он становится экспоненциальным, когда вы их гнездиваете. Вы должны использовать линейный макет с шириной = 0 и весом = 1 на элементе, который вы хотите заполнить слева.
Этот ответ лучше для производительности и практики. Помните: используйте относительную компоновку ТОЛЬКО, когда у вас нет другого выбора.
<?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">
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/prev_button"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="<" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:singleLine="true"
android:gravity="center"
android:text="TextView" />
<Button
android:id="@+id/next_button"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text=">" />
</LinearLayout>
</LinearLayout>
Ответ 9
Вы можете использовать layout_width
или layout_width
для 0dp
(по ориентации, которую вы хотите заполнить на оставшемся пространстве).
Затем используйте layout_weight
, чтобы заполнить оставшееся пространство.
Ответ 10
используйте Relativelayout для переноса LinearLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:round="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text="<"/>
<TextView
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:layout_weight = "1"/>
<Button
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text=">"/>
</LinearLayout>
</RelativeLayout>`
Ответ 11
я нашел
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginEnd="10dp"
android:fontFamily="casual"
android:text="(By Zeus B0t)"
'' android:textSize="10sp"
android:gravity="bottom"
android:textStyle="italic" />