Android 自动截断TextView文本,以免与另一个TextView重叠
我有一个Android 自动截断TextView文本,以免与另一个TextView重叠,android,xml,android-layout,Android,Xml,Android Layout,我有一个列表视图,它显示了一系列家庭作业。列表视图项使用框架布局定位两个文本视图。第一个TextView与左侧对齐,第二个与右侧对齐。(两者垂直对齐)第一个显示分配说明的片段,第二个显示截止日期 我想做的是让到期日占据所需的空间,而描述则占据剩余的空间,如下所示: |----------------------------------------------------| |阅读第15-35页,更新tim。。。5月4日,星期五 |---------------------------------
列表视图
,它显示了一系列家庭作业。列表视图
项使用框架布局
定位两个文本视图
。第一个TextView
与左侧对齐,第二个与右侧对齐。(两者垂直对齐)第一个显示分配说明的片段,第二个显示截止日期
我想做的是让到期日占据所需的空间,而描述则占据剩余的空间,如下所示:
|----------------------------------------------------||阅读第15-35页,更新tim。。。5月4日,星期五 |----------------------------------------------------| 现在,说明文字将继续与日期重叠。但它将在行的末尾截断 在设置
TextView
值(大概是在getView
调用中)之前,我是否可以在XML中这样做,还是必须在代码中这样做?如果我在代码中这样做,我必须计算字符串将占用的水平空间量,并计算出描述需要有多短。看起来可能会变得一团糟
对于如何实现这一目标的任何其他建议,我们将不胜感激 尝试使用省略号属性,如:
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"/>
请注意,Android或至少某些版本需要“ellipsize”和“singleline”属性,以便系统实际执行截断并添加省略号。这不是框架布局,而是线性布局或相对布局与省略号相结合的最佳位置:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
...
android:width="0dp"
android:height="wrap_content"
android:layout_weight="1"
android:ellipsize="end" />
<TextView
...
android:width="wrap_content"
android:height="wrap_content"
android:layout_weight="0" />
</LinearLayout>
或者交替地
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
...
android:id="@+id/secondTV"
android:width="wrap_content"
android:height="wrap_content"
android:layout_weight="0" />
<TextView
...
android:width="0dp"
android:height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/secondTV"
android:ellipsize="end"/>
</RelativeLayout>
将框架布局更改为线性布局或相对布局
- LinearLayout:将到期日宽度设为“包裹内容”,说明宽度设为0dp,然后在说明中添加布局\u weight=“1”
- RelativeLayout:首先使用width wrap_内容布局到期日,然后使用规则布局说明,说明应位于到期日的左侧
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/due_date"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:singleLine="true" />
<TextView android:id="@+id/description"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/due_date"
android:singleLine="true" />
</RelativeLayout>
(我需要先声明我的截止日期标签,以便在描述中引用它。我还刚刚意识到android:ellipsize似乎是可选的——我猜它默认为“end”。)
非常感谢 啊,修复了:我需要在Description TextView上设置layout_width=“0dp”。(是的,我现在意识到这是JRaymond说要做的…)不知道为什么它以前能工作…RelativeLayout选项为我做了!(仅供参考:布局重量仅适用于线性布局)这对我来说很有效。直到我指定了
singleLine
属性,才真正起作用