0

это в настоящее время моей строки макет для ListView:LinearLayout: Как правильно обернуть длинный текст без какой-либо разрывов строки

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

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:layout_marginLeft="5dp" 
     android:text="99.99.9999" 
     android:layout_weight="0.2"/> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_weight="0.6"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="10dp" 
      android:text="This is a correct looking text" 
      android:id="@+id/txtComment"/> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.2"> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="right" 
      android:layout_marginTop="10dp" 
      android:textStyle="bold" 
      android:layout_marginRight="5dp" 
      android:text="-1234,56" /> 
    </LinearLayout> 
</LinearLayout> 

Проблема заключается в TextView с идентификатором txtComment. Все хорошо выглядит для короткого текстового контента и текстового контента, который содержит регулярные разрывы строк.

Но как только будет длинный текст без перерывов на линии, Android все еще обертывает текст, а также изменяет вес окружающего LinearLayout так, чтобы сжатие левого и правого текста было сжато.

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

Скриншоты показывающие вопрос:

Short text Longer text with line breaks Longer text without line breaks

ответ

2

Попробуйте это,

Веса не работают должным образом, потому что вы установили ширину, wrap_content. Измените его на 0dp.

<TextView 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dp" 
    android:layout_marginLeft="5dp" 
    android:text="99.99.9999" 
    android:layout_weight="0.2"/> 

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:layout_weight="0.6"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="10dp" 
     android:text="This is a correct looking text" 
     android:id="@+id/txtComment"/> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.2"> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="right" 
     android:layout_marginTop="10dp" 
     android:textStyle="bold" 
     android:layout_marginRight="5dp" 
     android:text="-1234,56" /> 
</LinearLayout> 

+1

Эта работа отлично, спасибо – devnull69

1

вы просто должны установить ваш android:layout_width="wrap_content" в android:layout_width="0dip", где вы установите вес.

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

    <TextView 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:layout_marginTop="10dp" 
     android:layout_weight="0.2" 
     android:text="99.99.9999" /> 

    <LinearLayout 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.6" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/txtComment" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="10dp" 
      android:text="This is a correct looking textThis is a correct looking textThis is a correct looking textThis is a correct looking textThis is a correct looking text" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.2"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="5dp" 
      android:layout_marginTop="10dp" 
      android:gravity="right" 
      android:text="-1234,56" 
      android:textStyle="bold" /> 
    </LinearLayout> 
</LinearLayout> 

enter image description here

+1

отлично, спасибо. Раньше я ничего не слышал об этом. Полезно знать – devnull69

+0

здесь, в документах: https://developer.android.com/guide/topics/ui/layout/linear.html –

0

Дело в том, макет работает хорошо :-) Вы можете установить веса в 20%, 60% и 20%, что это именно то, что происходит в последнем случае.

Кроме того, вы должны установить android:layout_width="0dp" при использовании LineraLayout весов.

0

В корневом элементе: android:weightSum="1"

В ваших дочерних элементов android:layout_width="0dp"

0

Существует гораздо лучше и чище способ для достижения этой цели:

<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:padding="4dp" 
    app:alignmentMode="alignBounds" 
    app:columnCount="3" 
    app:rowOrderPreserved="false" 
    app:useDefaultMargins="true"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:layout_marginTop="10dp" 
     android:text="99.99.9999" 
     app:layout_gravity="fill_horizontal" /> 

    <TextView 
     android:id="@+id/txtComment" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:padding="10dp" 
     android:text="This is a correct looking textThis is a correct looking textThis is a correct looking textThis is a correct looking textThis is a correct looking text" 
     app:layout_columnWeight="3" 
     app:layout_gravity="fill_horizontal" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="5dp" 
     android:layout_marginTop="10dp" 
     android:gravity="right" 
     android:text="-1234,56" 
     android:textStyle="bold" 
     app:layout_gravity="fill_horizontal" /> 
</android.support.v7.widget.GridLayout> 

Также не забыли добавить в ваш файл градиента:

compile 'com.android.support:gridlayout-v7:24.1.0'

enter image description here

 Смежные вопросы

  • Нет связанных вопросов^_^