2012-04-17 93 views
0

我試圖完成的項目需求之一是重現類似於導航欄的iOS UI功能。居中顯示兩個可變寬度視圖父對齊的文本

(我知道這是不好的從Android設計的角度來看,但沒有什麼我可以做的。)

對於那些你們誰不知道我試圖完成here是一種例。關鍵是要有一個始終居中的TextView欄,同時可以將左右兩個可變寬度按鈕對齊。即使兩個按鈕之一消失,標題仍必須居中。

通過使用虛擬不可見視圖來強制左側和右側「組件」具有相同寬度,此行爲不是太難實現。

上述不起作用的用例是標題太長而不能以邏輯爲中心(或左側按鈕丟失)。在這種情況下,我的解決方案強制標題以橢圓化的方式快速。期望的行爲是讓標題延伸到空白空間。

任何幫助,將不勝感激。我意識到這個解釋可能相當混亂。我很樂意澄清任何觀點。

回答

0

你試過了RelativeLayout嗎? http://developer.android.com/reference/android/widget/RelativeLayout.html

使用RelativeLayout,您可以讓TextView填充該頂部欄的整個寬度,除了一些layout_margin左邊和右邊的按鈕留出空間。左側和右側按鈕將位於RelativeLayout的第二層(TextView下方)。

+0

自相矛盾,你的方法沒有考慮可變寬度按鈕。例如,如果沒有按鈕,我們有一個非常長的標題?在這種情況下,應該給TextView整個RelativeLayout的寬度。 – zienkikk 2012-04-17 04:02:44

0
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativeLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:text="Button" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:layout_toRightOf="@id/button1" 
     android:layout_toLeftOf="@id/button2" 
     android:ellipsize="middle" 
     android:text="TextView" /> 

</RelativeLayout> 
+0

糖果,我不認爲這很簡單。如果標題真的很長,該怎麼辦?我相信這種佈局會在左側按鈕和右側按鈕的右側顯示左側的標題,不是嗎?我的印象是,如果你使用layout_屬性來定位相對於父對象的視圖,那麼你將失去在RelativeLayout – zienkikk 2012-04-17 04:01:21

+0

內編輯代碼的能力。 – candyleung 2012-04-17 04:23:59