Неправильная позиция TextView в LinearLayout - PullRequest
0 голосов
/ 10 января 2019

У меня есть два TextView в LinearLayout:

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

    <TextView
        android:id="@+id/left_button"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_margin="4dp"
        android:layout_weight="1"
        android:background="#eeeeee"
        android:gravity="center"
        android:text="Yes"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/right_button"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_margin="4dp"
        android:layout_weight="1"
        android:background="#eeeeee"
        android:gravity="center"
        android:text="Very long long long long long text"
        android:textColor="#000000" />

</LinearLayout>

Это выглядит так: enter image description here

Если я удаляю android:gravity="center", это выглядит нормально. Но мне нужно это использовать. Как это исправить?

Ответы [ 4 ]

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

Попробуйте это -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="16dp">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:text="@string/yes"
            android:textSize="18sp"
            android:layout_margin="10dp"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:text="@string/very_long_text"
            android:textSize="18sp"
            android:layout_margin="10dp"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


    </LinearLayout>

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

Я предлагаю вам использовать weight_sum в вашем LinearLayout, а затем использовать ширину и высоту match_parent, тогда как значение EditText или weight=50 или более, зависит от того, какой текст вы хотите.

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

Попробуйте это

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

    <TextView
        android:id="@+id/left_button"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_margin="4dp"
        android:layout_weight="1"
        android:background="#eeeeee"
        android:gravity="center"
        android:text="Yes"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/right_button"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_margin="4dp"
        android:layout_weight="1"
        android:background="#eeeeee"
        android:gravity="center"
        android:text="Very long long long long long text"
        android:textColor="#000000" />

</LinearLayout>

Я добавил android:gravity="center" к родителю LinearLayout

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

Неправильная позиция TextView в LinearLayout

Вам необходимо установить android:gravity="center" в вашем LinearLayout

ОБРАЗЕЦ КОДА

<?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"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/feedback_binary_left_button"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_margin="4dp"
        android:layout_weight="1"
        android:background="#eeeeee"
        android:gravity="center"
        android:text="Yes"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/feedback_binary_right_button"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_margin="4dp"
        android:layout_weight="1"
        android:background="#eeeeee"
        android:gravity="center"
        android:includeFontPadding="false"
        android:text="Very long long long long long text"
        android:textColor="#000000" />

</LinearLayout>

OUTPUT

enter image description here

Также отметьте это, если хотите показать весь текст

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...