第一次在这个论坛发帖,希望一切顺利:)
我正在为我所在城市的公共交通开发 Android 应用程序。
这是我所拥有的 http://s28.postimg.org/i8cdifwgd/actual.png
[ |short destination ||next departure| ]
[ |way too long dest...||next departure| ]
这就是我想要的: http://s13.postimg.org/z6vfcr3fb/expected.png
[ |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,但没有成功:(
知道我该怎么做吗?
提前致谢!
Louloox
[解决了]
只需要稍微修改一下瓦尔贝托斯 answer:
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
再次感谢 :)