這是我第一次張貼在這個論壇上,希望它會沒事的:)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