这是我第一次张贴在这个论坛上,希望它会没事的:)2 TextViews,留下了省略号,右接NOWRAP,单线
我深化发展的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的,但没有成功:(
任何想法如何我能做到这一点?
提前感谢!
Louloox
[解决] 只要有轻轻修改valbertos答案:
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
再次感谢:)
下面是一个更完整的例子:http://s28.postimg.org/5gejnvfd9/actual2.png – louloox 2014-09-06 17:58:10
你必须understant那是什么,内容是动态地从一个WebService检索。因此,我不知道目的地名称有多大,所以我肯定需要“目标名称”TextView动态(根据宽度),我不希望这个“隐藏”右边TextView中。 – louloox 2014-09-06 18:00:24