【发布时间】:2014-10-31 08:21:15
【问题描述】:
这是我第一次在这个论坛上发帖,希望一切都会好起来:)
我正在为我所在城市的公共交通开发一款 Android 应用。
[ |short destination ||next departure| ]
[ |way too long dest...||next departure| ]
[ |short destination||next departure| ]
[ |way too long dest...||next departure| ]
这里有一个更完整的例子:s28.postimg.org/5gejnvfd9/actual2.png
奇怪的彩色背景只是为了轻松识别布局/文本视图。您也可以忽略棕色线(没关系)。
基本上,我想要具有可变长度的目的地[红色背景],并且在其右侧,我想要第一个出发时间[绿色背景]。一切都在一条线上。
我需要始终完全显示第一个出发信息(nowrap)。目的地可以用省略号 (...) 包裹。 [可选问题,如何将省略号 '...' 替换为 '.' ?]
这是迄今为止我最好的工作代码:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/txtTitleDestination"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/txtTitleFirstDeparture"
android:background="#FF0000"
android:ellipsize="end"
android:maxLines="1"
android:padding="0dp"
android:scrollHorizontally="true"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/txtTitleFirstDeparture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_alignParentRight="true"
android:background="#00FF00"
android:ellipsize="none"
android:maxLines="1"
android:padding="0dp"
android:textSize="18sp"
android:textStyle="bold"
/>
</RelativeLayout>
我尝试过 TableLayout 和 LinearLayour 而不是 RelativeLayout,但没有成功:(
知道我该怎么做吗?
提前致谢!
卢鲁克斯
[已解决] 只需轻轻修改 valberos 答案:
titleDestination.post(new Runnable() {
@Override
public void run() {
int widthTextViewDeparture = measureTextWidthTextView(titleFirstTime, pContext);
int widthTextViewDestination = titleDestination.getWidth();
int widthTextViewParent = rl_parent.getWidth();
if(widthTextViewDestination + widthTextViewDeparture > widthTextViewParent) {
titleDestination.setWidth(widthTextViewParent - widthTextViewDeparture);
titleDestination.setEllipsize(TruncateAt.END);
titleDestination.setHorizontallyScrolling(true);
}
}
});
仅在必要时设置省略号会使文本正确截断。
Before:
Lingolsheim Thiergaten --> Lingolsheim... [1'23"] 21h23
With the modification:
Lingolsheim Thiergaten --> Lingolsheim Thi... [1'23"] 21h23
再次感谢:)
【问题讨论】:
-
这里有一个更完整的例子:s28.postimg.org/5gejnvfd9/actual2.png
-
您必须了解的是,内容是从 WebService 中动态检索的。因此,我事先不知道目的地名称有多大,所以我肯定需要动态的“目的地名称”TextView(就宽度而言),我不希望这个“隐藏”右边文本视图。
标签: android textview ellipsis nowrap