Текст справа и по центру вертикали другого текстового поля

У меня два textView. Один - держатель текста, второй - держатель счетчика. Когда текстовый держатель имеет одну ширину, все кажется правильным. Но когда текст состоит из двух и более строк, мое количество textview покидает экран. (смотреть экраны). Я пробовал относительные и ограничения, но все не работает

<?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"
    xmlns:tools = "http://schemas.android.com/tools"
    android:background = "?android:attr/selectableItemBackground"
    android:clickable = "true"
    android:focusable = "true"
    android:orientation = "horizontal"
    android:padding = "@dimen/margin_general_16dp">

    <TextView
        android:id = "@+id/testText"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:paddingStart = "@dimen/margin_general_16dp"
        android:textColor = "@color/text_color_general"
        android:textSize = "@dimen/text_size_general"
        tools:text = "Test test test Test test test Test test test Test test test Test test test Test test test"/>

    <TextView
        android:id = "@+id/testAmount"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_marginStart = "@dimen/margin_small_8dp"
        android:textColor = "@color/redorange"
        android:textSize = "@dimen/text_size_general"
        tools:text = "50"/>
</LinearLayout>
0
0
30
2

Ответы 2

Используйте следующий код:

<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent"
    xmlns:tools = "http://schemas.android.com/tools"
    android:orientation = "horizontal">
    <TextView
        android:id = "@+id/testText"
        android:layout_width = "0dp"
        android:layout_height = "wrap_content"
        android:layout_weight = "1"
        tools:text = "Test test test Test test test Test test test Test test test Test test test Test test test"/>

    <TextView
        android:id = "@+id/testAmount"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_marginStart = "8dp"
        tools:text = "50"/>
</LinearLayout>

Используйте атрибут android:layout_weight.

Установите вес TextViews в соответствии с вашими потребностями.

Попробуйте под кодом:

<?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"
    xmlns:tools = "http://schemas.android.com/tools"
    android:clickable = "true"
    android:focusable = "true" android:orientation = "horizontal"
    android:gravity = "center_vertical"
    android:padding = "@dimen/margin_general_16dp">

    <TextView
        android:id = "@+id/testText"
        android:layout_weight = ".5"
        android:layout_width = "0dp"
        android:layout_height = "wrap_content"
        android:paddingStart = "@dimen/margin_general_16dp"
        android:textColor = "@color/text_color_general"
        android:textSize = "@dimen/text_size_general"
        android:text = "Test test test Test test test Test test test Test test test Test test test Test test test"
        android:paddingLeft = "@dimen/margin_general_16dp" />

    <TextView
        android:id = "@+id/testAmount"
        android:layout_weight = ".5"
        android:layout_width = "0dp"
        android:layout_height = "wrap_content"
        android:layout_marginStart = "@dimen/margin_small_8dp"
        android:textColor = "@color/redorange"
        android:textSize = "@dimen/text_size_general"
        android:text = "50"
        android:layout_marginLeft = "@dimen/margin_small_8dp" />
</LinearLayout>

Я надеюсь, что это работа для вас.

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