Обрезать текстовое представление в LinearLayout
У меня есть LinearLayout с двумя TextView в нем - textName
а также textPrice
, Свойства textName
:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
и свойства textPrice
:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
В случае когда textName
длинный, он растягивается по всей линии и закрывает textPrice
,
Вот моя xml-разметка:
<LinearLayout
android:id="@+id/textNameTimeHolder"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail" android:layout_alignBottom="@+id/thumbnail">
<TextView
android:id="@+id/textName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Неудержимые 3"
android:textColor="#040404"
android:typeface="sans"
android:textSize="15sp"
android:textStyle="bold"
android:singleLine="true"
android:layout_toLeftOf="@+id/textPrice"/>
<TextView
android:id="@+id/textPrice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="15sp"
android:text="150 - 220 сом"
android:layout_marginRight="5dp"
android:layout_gravity="right"/>
</LinearLayout>
Как я могу обрезать textName
(с помощью singleline
) когда это слишком долго?
Благодарю.
4 ответа
Решение
Задавать ellipsize
собственность в вашем TextView
и лучше использовать weight
в обоих TextView
<LinearLayout
android:id="@+id/textNameTimeHolder"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:layout_alignBottom="@+id/thumbnail">
<TextView
android:id="@+id/textName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:text="Неудержимые 3"
android:textColor="#040404"
android:typeface="sans"
android:textSize="15sp"
android:textStyle="bold"
android:singleLine="true"/>
<TextView
android:id="@+id/textPrice"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:gravity="right"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="15sp"
android:text="150 - 220 сом"
android:layout_marginRight="5dp"
android:layout_gravity="right"/>
</LinearLayout>
Ты можешь использовать android:maxEMS
(EMS - это максимальное пространство, занимаемое символом, технически пространство, занимаемое символами E или M), и выберите конкретную длину. Если текст слишком длинный, он будет автоматически дополнен тремя точками (...).
Вы должны установить android:ellipsize
на ваш просмотр текста.