Слишком длинный текст в LinearLayout отталкивает ImageView - PullRequest
0 голосов
/ 18 октября 2019

В конце LinearLayout есть ImageView, отделенный Space от TextView. Когда текст в TextView не слишком длинный, ImageView выглядит хорошо выровненным по правому краю. Но когда текст слишком длинный, он съедает дыру в LinearLayout и ImageView отталкивается.

Как мне этого избежать? Я бы хотел, чтобы текст становился эллиптическим прямо перед ImageView, чтобы ImageView всегда был виден.

Это мой код:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:ellipsize="end"
            android:layout_margin="20sp"
            android:textSize="20sp"
            android:id="@+id/text"/>
    <Space
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
    <ImageView
            android:src="@drawable/ic_star_gold_24dp"
            android:layout_margin="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
</LinearLayout>

Ответы [ 3 ]

2 голосов
/ 18 октября 2019

Похоже, вы хотите установить вес в TextView, чтобы он заполнил все доступное пространство. Можете ли вы попробовать что-то вроде этого?

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

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20sp"
        tools:text="This is some really long sample text"
        android:ellipsize="end"
        android:maxLines="1"
        android:layout_weight="1"
        android:textSize="20sp" />

    <Space
        android:layout_width="1dp"
        android:layout_height="match_parent"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20sp"
        android:src="@drawable/ic_star_gold_24dp" />

</LinearLayout>
0 голосов
/ 18 октября 2019

У вас есть несколько вариантов решения этой проблемы:

  1. Установите вес на TextView, опционально установите фиксированную ширину для Space и опционально установите фиксированную ширину, высоту дляImageView

    <TextView
        android:id="@+id/text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="20sp"
        tools:text="This is some really long sample text"
        android:ellipsize="end"
        android:maxLines="1"
        android:layout_weight="1"
        android:textSize="20sp" />
    
    <Space
        android:layout_width="20dp"
        android:layout_height="match_parent"/>
    
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20sp"
        android:src="@drawable/ic_launcher" />
    

  2. Замените LinearLayout на ConstraintLayout и используйте Barrier для ограничения ширинываш TextView.

Проверьте эту потрясающую страницу , чтобы узнать, как использовать Barrier в ConstraintLayout

Чтобы ознакомиться с ConstraintLayout, вы можете проверить эту кодовую метку ИспользованиеConstraintLayout

0 голосов
/ 18 октября 2019

Используйте ImageView с фиксированным размером и TextView с таким весом -

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:layout_weight="1"
            android:ellipsize="end"
            android:layout_margin="20sp"
            android:textSize="20sp"
            android:id="@+id/text"/>
    <ImageView
            android:src="@drawable/ic_star_gold_24dp"
            android:layout_margin="20sp"
            android:layout_width="30dp"
            android:layout_height="30dp"/>
</LinearLayout>
...