2013-01-16 68 views
0

我有一個相當簡單的佈局麻煩斑點。那就是:佈局麻煩

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/id1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:ellipsize="end" 
     android:maxLines="1"/> 

    <TextView 
     android:id="@+id/id2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall"/> 
</LinearLayout> 

3個問題:

  • 如何中心垂直兩個TextViews(也許還好說,這些觀點中的文本)母公司的LinearLayout內?左視圖垂直居中確定,但正確的(因爲它具有較小的字體)不是。它似乎在頂部垂直居中。我顯然嘗試玩第二種視圖的layout_gravity,但這沒有任何區別。我能解決這個問題的唯一方法是將第二個TextView放在一個LinearLayout中,並將其佈局高度參數設置爲match_parent(但這是否是正確的方式?)

  • 同樣,我希望左側的視圖水平居中在左側,右側的視圖水平居中在右側。目前右側視圖立即放置在左側旁邊。我可以解決這個問題的唯一方法是將這樣的插圖中的兩個文本觀點:

    <TextView 
        android:layout_width="0dip" 
        android:layout_height="fill_parent" 
        android:layout_weight="1" 
        android:text=""> 
    

    基本上作爲它的大小取決於文本的legth兩個TextViews減少間隔

  • 如果視圖的組合文本不能水平放置,我希望左側視圖中的文本被截斷。沒有包裝到新的生產線上。目前,左側視圖中的文字只是將「正確」的文字從父文件中「推出」。不知道如何實現這一點(除了添加android:maxLines="1"來阻止文本換行)。我試過android:ellipsize="end"但這似乎沒有任何影響。

+1

在線性佈局中設置android:gravity =「center_vertical」。 –

+0

謝謝。這回答了第一部分! – Lieuwe

回答

2

最好的辦法是使用相對佈局,但仍,如果你想要做的線性佈局同樣的事情比你的XML文件做一些改變 - 首先設置線性佈局等高,匹配父:

<LinearLayout android:layout_width="fill_parent" 
android:layout_height="match_parent" 
android:orientation="horizonatal"> 

- 第二製造的意見可見的中心垂直做

android:layout_gravity="center_vertical" 

同一屬性的android:layout_gravity =「CENTER_HORIZONTAL」,你必須添加硒cond文本視圖也。

這將使你的兩個文本視圖出現在中心垂直,但一個接一個。 爲了讓第二個視圖出現在右邊我認爲你可以添加 android:layout_marginLeft =「xx dp」 放置一些值來代替xx。

對於關於截斷文本的第三個問題,您應該給TextView的一些大小而不是包裝內容..像android:layout_width ="25dp" 然後使用android:ellipsize="end"

我想你會得到那個..其實我很着急,有時間離開辦公室。