Горизонтальный LinearLayout с тремя видами, средний вид должен быть горизонтально центрирован - PullRequest
0 голосов
/ 04 января 2019

У меня есть три элемента по горизонтали LinearLayout: TextView, ImageButton и еще один TextView. Все три вертикально центрированы.

enter image description here

Как можно дополнительно добиться того, чтобы середина ImageButton была точно центрирована по горизонтали независимо от ширины двух TextViews?

То есть: я хочу, чтобы ImageButton находился в точном центре, левый TextView должен выглядеть выровненным по правому краю, а правый TextView должен быть выровненным по левому краю. Все три вида должны быть вертикально центрированы.

Спасибо.

Stefan

Ответы [ 5 ]

0 голосов
/ 05 января 2019

Пожалуйста, попробуйте это ...

<?xml version="1.0" encoding="utf-8"?><?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="wrap_content"
    android:gravity="center_vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="end"
        android:text="This is a first text" />

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="start"
        android:text="This is second text" />
</LinearLayout>
0 голосов
/ 04 января 2019

Использовать вес макета. Проверьте приведенный ниже пример:

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

       <TextView
           android:id="@+id/text1"
           android:layout_width="@dimen/dimen_0dp"
           android:layout_weight="1"
           android:gravity="center_vertical"
           android:layout_height="match_parent"
           android:text="text1"
           />
        <ImageView
            android:id="@+id/b"
            android:layout_width="@dimen/dimen_0dp"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:src="@drawable/ic_delete_button"
            android:layout_weight="1"
            />
        <TextView
            android:id="@+id/text2"
            android:layout_width="@dimen/dimen_0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="text2"/>

    </LinearLayout>
0 голосов
/ 04 января 2019

Использование в LinearLayout:

android:weightSum=“3”

и на каждом макете из вашего 3 макета используйте атрибут ниже

android:layout_weight=“1”
android:layout_width=“0dp”

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3"
tools:context=".MainActivity">

<TextView
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content" />

<ImageButton
    android:layout_weight="1"
    android:layout_width="0"
    android:layout_height="wrap_content" />

<TextView
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content" />



</LinearLayout>

Пожалуйста, проверьте официальные документы Android, чтобы проверить дополнительные атрибуты LinearLayout: https://developer.android.com/reference/android/widget/LinearLayout

0 голосов
/ 04 января 2019

Дети должны иметь:

1 - layout_width = "0dp"

2 - layout_weight = "1"

Это реализация, которая вам нужна:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="This is a first text" />

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        tools:srcCompat="@drawable/avatar" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="This is second text that has a long content than the first one" />

</LinearLayout>
0 голосов
/ 04 января 2019

вам лучше установить вес для них. просто играть с весом.

если они имеют постоянный вес, они всегда заполняют определенное количество места, несмотря ни на что.

1 - установить ширину всех видов в соответствии с родителями.

2 - установите весь вес на 1.

затем убедитесь, что ваше изображение находится в центре этих текстовых представлений

 <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="top"
        android:hint="@string/message" />
...