這很難解釋,請耐心等待。將兩個按鈕之間的屏幕中心對齊文本,其中一個可能不可見
我有一個頂級菜單欄,在設計中已經給我(這是一個端口)。
菜單欄有兩個按鈕,一個在菜單的左側,一個在右側,兩個按鈕之間有文本標題。
文本視圖是單行,所以如果文本比視圖更長,它會在最後使用省略號縮短。文本應居中在按鈕之間的顯示寬度內。文本是當前內容的標題,可以縮短。實際上,在大多數設備上,只有一兩個奇數字被切掉。
如果兩個按鈕都可見,那麼它非常簡單,但取決於我在應用程序中的位置,右側按鈕可能不可見。當它不可見時,我使用View.GONE。現在的問題是,文本現在填充了所需的剩餘空間,但居中在剩餘空間中,而不是在顯示寬度內,因此短片段的文本右移了第一個按鈕的寬度。
使用View.INVISIBLE而不是View.GONE並不是答案,因爲文本會正確居中,但不會使用可用空間來最小化文本的縮短。
這裏是我想要達到的效果:
兩個按鈕可見:
---------------------------------------------------------------
| Left button | Title text centred in display | Right button |
---------------------------------------------------------------
右按鈕隱藏
---------------------------------------------------------------
| Left button | Title text centred in display |
---------------------------------------------------------------
右按鈕隱藏着長文本顯示省略號。這是我正在尋找的。
---------------------------------------------------------------
| Left button | A really long piece of title text centred ...|
---------------------------------------------------------------
下面是我在隱藏右鍵時注意到文本不居中。
---------------------------------------------------------------
| Left button | Title text centred in this space |
---------------------------------------------------------------
這是我目前使用的佈局。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/activitytitlegradient"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<ImageView android:id="@+id/mainMenuLeftButton"
android:background="#00ffffff"
android:layout_gravity="center_vertical"
android:src="@drawable/rr_boat_bow"
android:scaleType="fitStart"
android:layout_weight="1"
android:layout_height="34dip"
android:layout_width="wrap_content"/>
<TextView android:id="@+id/mainMenuTitle"
android:layout_weight="10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="2dp"
android:background="#00ffffff"
android:text="Reference"
android:singleLine="true"
android:textSize="19sp"
android:textStyle="bold"
android:textColor="#ffffff"
android:gravity="center_horizontal|center_vertical" />
<Button android:id="@+id/mainMenuRightButton"
android:background="@drawable/buttonborder"
android:text="Edit"
android:textSize="13sp"
android:textColor="#ffffff"
android:textStyle="bold"
android:layout_margin="3dip"
android:padding="3dip"
android:layout_weight="1"
android:layout_height="35dip"
android:layout_width="wrap_content"
android:visibility="gone"/>
</LinearLayout>
第一個按鈕是作爲一個ImageView實現的,以啓用我在其他地方做的其他一些(不相關的)效果。該文本是硬編碼只是爲了幫助鋪設。我的代碼根據需要更改文本。
我可以用自定義視圖來做到這一點,但我確定必須有一種方法可以用標準框架來做到這一點。
謝謝你的任何線索......
謝謝約翰,但RelativeLayout沒有幫助。我編輯了我的OP,試圖澄清我正在尋找的效果。歡呼 – Simon 2012-01-29 09:34:00
RightButton應該是「隱形」不會消失(對不起,忽略這一點)。你可以給TextView的寬度一個固定的值。我的想法是,把左邊的左鍵,把右邊的右邊,並把textView右邊的leftButton(...所以我錯了在響應它應該是androd:layout_toRightOf =「@ id/mainMenuLeftButton」 ),嘗試給textView一個固定的寬度?它應該工作 – JohnCookie 2012-01-29 09:54:02
再次感謝約翰,但仍然不是我要找的。我已經給我的OP添加了更多的說明:)使用任何固定寬度(除wrap_content或fill_parent之外的任何其他內容)的問題是它不會居中文本。它適用於短文本,但如果內容比TextView更寬,則會失敗。此外,View.INVISIBLE不是有用的,因爲視圖的寬度仍然用於佈置如此長的文本(導致elipsis)不會居中顯示寬度,但會居中在兩個按鈕之間工作,但會不讓我使用全部可用寬度 – Simon 2012-01-29 10:25:03