Как сделать TextView в LinearLayout заполнить оставшееся пространство и быть усеченным - PullRequest
0 голосов
/ 13 февраля 2019

У меня есть ListView, содержащий несколько строк, особенно строку, объединяющую категории и расстояние элементов в 2 TextView:

  • элемент category может содержать одну или несколько категорий, поэтому этот TextView должен быть усеченный
  • элемент distance отображается только в том случае, еслипользователь разрешил геолокацию, поэтому TextView может быть скрыт
  • , в этом случае элемент category должен заполнить всю ширину строки

Ожидаемый результат выглядит следующим образом: Снимок экрана приложения «Карты» на iOS: Maps screenshot

Я пытался сделать это с помощью LinearLayout,но это не работает, как ожидалось:

<LinearLayout android:id="@+id/AffiliateCellDetail"
              android:layout_below="@id/AffiliateCellTitle"
              android:layout_height="wrap_content"
              android:layout_width="match_parent" >
    <TextView android:id="@+id/AffiliateCellCategories"
              android:layout_height="wrap_content"
              android:layout_width="wrap_content"
              android:ellipsize="end"
              android:singleLine="true"/>        
    <TextView android:id="@+id/AffiliateCellDistance"
              android:layout_height="wrap_content"
              android:layout_width="wrap_content"/>         
</LinearLayout>   

Есть ли лучший подход для достижения этого?

Ответы [ 2 ]

0 голосов
/ 13 февраля 2019

Я наконец-то достиг этого, используя ConstraintLayout:

<android.support.constraint.ConstraintLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/AffiliateCellDetail"
        android:layout_below="@id/AffiliateCellTitle"
        android:layout_height="wrap_content"
        android:layout_width="match_parent" >
    <TextView android:id="@+id/AffiliateCellCategories"
              android:layout_height="wrap_content"
              android:layout_width="0dp"
              android:ellipsize="end"
              android:singleLine="true"
              app:layout_constraintHorizontal_chainStyle="packed"
              app:layout_constraintHorizontal_bias="0"
              app:layout_constraintWidth_default="wrap"
              app:layout_constraintTop_toTopOf="parent"
              app:layout_constraintLeft_toLeftOf="parent"
              app:layout_constraintRight_toLeftOf="@+id/AffiliateCellDistance"/>        
    <TextView android:id="@+id/AffiliateCellDistance"
              android:layout_height="wrap_content"
              android:layout_width="wrap_content"
              app:layout_constraintBaseline_toBaselineOf="@+id/AffiliateCellCategories"
              app:layout_constraintLeft_toRightOf="@+id/AffiliateCellCategories"
              app:layout_constraintRight_toRightOf="parent"/>                
</android.support.constraint.ConstraintLayout>
0 голосов
/ 13 февраля 2019

Попробуйте:

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

    <TextView
        android:id="@+id/AffiliateCellCategories"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ellipsize="end"
        android:singleLine="true" />

    <TextView
        android:id="@+id/AffiliateCellDistance"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

android:layout_weight="1" расширит категорию TextView, когда будет место для заполнения.

...