Выравнивание двух текстовых представлений в виде списка

Вот что у меня есть:

Есть варианты, и есть содержание для каждой опции. Номера опций (A, B, C, D) находятся в отдельном текстовом представлении, а их содержимое - в отдельном текстовом представлении.

Все, что я пытаюсь, это выравнивание номера опции с первой строкой содержания опции. Я пытался установить постоянную paddingTop, но по мере изменения количества строк в содержании опции оно выходит из выравнивания. Любые предложения о том, как это сделать?

Вот часть XML:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/options"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp">

    <TextView
        android:id="@+id/option_number"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="10"
        android:text="A"
        android:paddingLeft="5dp"
        android:textSize="17sp"
        android:textStyle="bold"
        android:textColor="@color/charcoal"/>



    <TextView
        android:id="@+id/option_content"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="100"
        android:textSize="17sp"
        android:gravity="center|left"
        android:paddingRight="20dp"
        android:text="option content...."
        android:textColor="@color/charcoal"/>

Могу ли я сделать это, получив paddingTop содержимого опции динамически и установка того же отступа для номера опции?

5 ответов

Решение

Попробуйте с ниже.

Замените это в своем XML.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/options"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum="5">

        <TextView
            android:id="@+id/option_number"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingLeft="5dp"
            android:text="A"
            android:maxLines="4"
            android:textColor="@color/charcoal"
            android:textSize="17sp"
            android:textStyle="bold" />


        <TextView
            android:id="@+id/option_content"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:maxLines="4"
            android:text="option content...."
            android:textColor="@color/charcoal"
            android:textSize="17sp" />

    </LinearLayout>

И это сделано.

Конечно, это поможет вам.

Установите android:gravity="top" для option_number TextView android:gravity="top|left" для option_content. Или используйте Относительный макет и используйте layout_aligntop

Использовать: android:layout_centerVertical="true",

Предложение, можно использовать следующие три в сочетании, чтобы легко достичь того, что вы хотите:
1) CenterVertical
2) ToRightOf
3) RelativeLayout.

например:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/options"
    android:layout_width="match_parent"
    android:layout_height="80dp">

    <TextView
        android:layout_centerVertical="true"

        android:id="@+id/option_number"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:text="A"
        android:paddingLeft="5dp"
        android:textSize="17sp"
        android:textStyle="bold"
        android:textColor="@color/charcoal"/>

    <TextView
        android:layout_toRightOf="@id/option_number"
        android:layout_centerVertical="true"

        android:id="@+id/option_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17sp"
        android:text="option content...."
        android:textColor="@color/charcoal"/>

Вы можете использовать Относительное расположение вместо линейного, но вам придется немного изменить свой XML. Так что, если вы решите использовать его, вы можете просто установить следующее свойство для первого TextView

android:layout_alignTop="@+id/option_content"

Это должно помочь вам.

Попробуйте установить android:layout_alignBaseline="@id/option_number" на option_content

Другие вопросы по тегам