[英]How to ellipsize center-aligned textview beside a right-aligned button when screen width is small
------------------------------------ | LONG_TITL... |button| | |------------------------------------| | |
我有一個由應用程序標題和按鈕組成的應用程序標頭。 該按鈕應該固定在右側,但是當屏幕太小時,我希望標題位於中間並且為橢圓形。 問題是,即使按鈕幾乎在標題旁邊,標題也不會省略。
這是我到目前為止的內容:
<RelativeLayout android:id="@+id/headr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<ImageView
android:id="@+id/bar_orange"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:minHeight="40dp"
android:scaleType="centerCrop"
android:src="@drawable/bar_orange"/>
<TextView
android:id="@+id/Title"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="@string/bcd"
android:textColor="#FFFFFF"
android:ellipsize="marquee"
android:singleLine="true"
android:textSize="18sp"
android:textStyle="bold"/>
<Button
android:padding="5dp"
android:id="@+id/home"
android:textColor="#ffffff"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:singleLine="true"
android:background="@drawable/button_home"
android:text="@string/home"/>
</RelativeLayout>
是否只有RelativeLayout
才能做到這一點?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toLeftOf="@+id/button"
android:ellipsize="end"
android:singleLine="true"
android:text="very large center align text very large center align text very large center align text" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Right button" />
</RelativeLayout>
</RelativeLayout>
將此添加到您的textView
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
在TextView中更改為android:ellipsize="end"
。
為TextView設置固定寬度
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.