Android TextView截断优先级或抗压缩性 [英] Android TextView Truncation Priority or Compression Resistance
问题描述
我正在屏幕上水平放置3个视图(固定大小的图像和2个单行文本视图:leftTextView
和rightTextView
),并且我试图使rightTextView
紧靠屏幕leftTextView
,但是如果两个标签的宽度都超过了屏幕尺寸,请截断leftTextiew
.
I'm laying out 3 views horizontally on the screen (fixed-size image and 2 single-line text views: leftTextView
and rightTextView
) and I'm trying to get the rightTextView
to hug against the leftTextView
, but in the event that the width of both labels would exceed the screen size, truncate the leftTextiew
.
所需功能示例:
|img|leftText|rightText| ||(end of screen)
|img|leftTextMedium|rightText| ||
|img|leftTextTooLongSoTrunc...|rightText||
实际发生的事情:
|img|leftText|rightText| ||(end of screen)
|img|leftTextPrettyLongButNotHuge|rightT||
|img|leftTextWhichIsIncrediblyLongBlahBl||
当前,如果两个文本视图的大小都超过了视图的宽度,则即使我在leftTextView
上设置了android:ellipsize="end"
,rightTextView
最终仍会被压缩以腾出空间.
Currently, if the size of both text views exceeds the view's width, the rightTextView
ends up getting squished down to make room, even though I've set android:ellipsize="end"
on the leftTextView
.
是否有一种方法可以使leftTextView
具有截断优先级",该截断优先级表示如果将导致其他视图不适合,则应将其截断?或者在另一面,我可以将rightTextView
设置为具有抗压缩性",以防止压缩它为leftTextView
腾出空间吗?这是我的XML的示例:
Is there a way to make the leftTextView
have a "truncation priority" that says it should truncate if it would cause other views to not fit? Or on the other side, can I set the rightTextView
to have a "compression resistance" in order to keep it from being squished to make room for the leftTextView
? Here's a sample of my XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/fixedWidthImage"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/leftTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/fixedWidthImage"
android:layout_toRightOf="@id/fixedWidthImage"
android:maxLines="1"
android:textSize="18sp"
android:ellipsize="end" />
<TextView
android:id="@+id/rightTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/leftTextView"
android:layout_toRightOf="@+id/leftTextView"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:maxLines="1"
android:textSize="12sp" />
</RelativeLayout>
我尝试过的事情:
- 在
leftTextView
中,添加toStartOf
/toLeftOf
rightTextView
(崩溃应用程序) -
而不是将
rightTextView
对齐到leftTextView
的结尾/右边,而是将其翻转并将leftTextView
对齐到rightTextView
的开头/左边.这导致rightTextView
锚定在屏幕的末尾,而不是直接锚定在leftTextView
的右边.最终的外观示例:
- In the
leftTextView
, add atoStartOf
/toLeftOf
therightTextView
(crashes the app) Instead of aligning the
rightTextView
to the end/right of theleftTextView
, flip it and align theleftTextView
to the start/left of therightTextView
. This results in therightTextView
being anchored to the end of the screen instead of directly to the right of theleftTextView
. Example of how this ends up looking:
|img|leftText |rightText||
|img|leftTextMedium |rightText||
|img|leftTextTooLongSoTrunc...|rightText||
即使我设置android:minWidth="25dp"
只是为了确保至少可见rightTextView
的 part (我也不愿意这样做,因为此文本长度是可变的),仍然最终会压缩宽度以为leftTextView
腾出空间.
Even if I set android:minWidth="25dp"
just to ensure at least part of the rightTextView
is visible (which I don't want to do because this text length is variable) it still ends up compressing the width to make room for the leftTextView
.
推荐答案
您可以通过TableLayout
和shrinkColumns
属性将此布局存档.
You can archive this layout by TableLayout
and shrinkColumns
attribute.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Row 1 -->
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="1">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:maxLines="1"
android:ellipsize="end"
android:text="leftText"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:maxLines="1"
android:ellipsize="none"
android:text="rightText"/>
</TableRow>
</TableLayout>
<!-- Row 2 -->
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="1">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:maxLines="1"
android:ellipsize="end"
android:text="leftTextPrettyLongButNotHuge"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:maxLines="1"
android:ellipsize="none"
android:text="rightText"/>
</TableRow>
</TableLayout>
<!-- Row 3 -->
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="1">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:maxLines="1"
android:ellipsize="end"
android:text="leftTextWhichIsIncrediblyLongBlahBlahBlahBlah"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:maxLines="1"
android:ellipsize="none"
android:text="rightText"/>
</TableRow>
</TableLayout>
</LinearLayout>
这篇关于Android TextView截断优先级或抗压缩性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!