Как я могу отобразить длинную высоту TextView в строке ListView?

#java #android #listview #textview #android-linearlayout

#java #Android #listview #textview #android-linearlayout

Вопрос:

У меня есть ListView, который выглядит следующим образом: введите описание изображения здесь

Обратите внимание, как TextView прерывается пропусками. Как я могу убедиться, что весь TextView виден внутри ListView?

Вот мой XML для строки (вызывается textview bodyTextView ):

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="horizontal"
      android:padding="4px">
      <ImageView android:id="@ id/avatarImageView"
          android:layout_width="48px"
          android:layout_height="48px"/>
      <LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:paddingLeft="4px">
          <LinearLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:layout_weight="1"
             android:gravity="center_vertical">
              <TextView android:id="@ id/usernameTextView"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:gravity="left"
                 android:textStyle="bold"
                  android:singleLine="true"
                  android:ellipsize="end"
                  android:textColor="#444444"
                  android:padding="0px"/>

              <TextView android:id="@ id/bodyTextView"
                 android:orientation="vertical"
                 android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:singleLine="false"
                  android:textColor="#666666"
                  android:maxLines="5"
                  android:ellipsize="end"/>
          <TextView android:id="@ id/dateTextView"
                 android:orientation="vertical"
                 android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:singleLine="true"/>
          </LinearLayout>
      </LinearLayout>
  </LinearLayout>
  

Ответ №1:

Установите ellipsize = «none». Кроме того, не забудьте указать достаточно большие maxLines.

Ответ №2:

На самом деле, ellipsize =»none» не повлияет на ширину элемента.

Чтобы заставить элемент отображать его полное содержимое в LinearLayout, вам нужно установить для его layout_width или layout_height (в зависимости от того, что применимо) значение «wrap_content», а затем убедиться, что другие элементы в LinearLayout не будут занимать пространство первыми. Даже с этой опцией однострочный TextView все равно будет иметь многоточие, как только он заполнит ширину окружающего макета, конечно.

Как убедиться, что другие элементы воспроизводятся нормально? Убедитесь, что для них также не установлено значение fill_* option или wrap_content. Простой способ отобразить несколько текстовых представлений, при котором одно из них занимает все необходимое пространство, а остальные работают нормально, заключается в следующем: установите для основного значения layout_width значения «wrap_content» с layout_weight значения «0» (это значение по умолчанию, его устанавливать не нужно), а затем установите для остальных значения layout_width значения «0dip» с layout_weight значения «1». Это даст первичному TextView макет с установленной шириной, соответствующей содержимому, а другим TextViews — эффективную ширину, равную нулю. Затем макет увидит layout_weights и изменит размер других TextViews, чтобы занять оставшееся пространство.