2012-09-24 70 views
2

我的佈局是這樣的:對齊TextViews與RelativeLayout的

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="@drawable/bgr"> 
    <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" 
      > 
     <TextView android:layout_width="60dp" android:layout_height="wrap_content" 
        android:id="@+id/label" 
        style="@style/ConnectionPoint.Label" 
        android:text="Title" 
       /> 
     <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" 
        android:id="@+id/code" 
        style="@style/ConnectionPoint.Code" 
        android:text="CODE" 
       /> 
     <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" 
        android:id="@+id/name" 
        android:layout_toRightOf="@id/label" 
        android:layout_toLeftOf="@id/code" 
        style="@style/ConnectionPoint.Name" 
        android:text="Some text" 
       /> 
    </RelativeLayout> 
</RelativeLayout> 

而且款式:

<style name="ConnectionPoint.Text" parent="android:TextAppearance"> 
    <item name="android:layout_alignParentBottom">true</item> 
</style> 
<style name="ConnectionPoint.Label" parent="ConnectionPoint.Text"> 
    <item name="android:textColor">@color/from_to</item> 
    <item name="android:layout_marginLeft">5dp</item> 
    <item name="android:layout_marginRight">10dp</item> 
    <item name="android:textSize">16dp</item> 
</style> 
<style name="ConnectionPoint.Name" parent="ConnectionPoint.Text"> 
    <item name="android:textSize">26dp</item> 
    <item name="android:textStyle">bold</item> 
    <item name="android:singleLine">true</item> 
    <item name="android:ellipsize">end</item> 
</style> 
<style name="ConnectionPoint.Code" parent="ConnectionPoint.Text"> 
    <item name="android:textColor">@color/iata</item> 
    <item name="android:textSize">18dp</item> 
    <item name="android:singleLine">true</item> 
    <item name="android:layout_alignParentRight">true</item> 
    <item name="android:layout_marginRight">5dp</item> 
    <item name="android:layout_marginLeft">5dp</item> 
</style> 

但在結果TextViews沒有正確對齊:

enter image description here

我希望它們按照底部對齊,並且無法理解爲什麼RelativeLayout無法做到這一點。我應該怎麼做RelativeLayout對齊TextViews?

+0

從圖像看,它們出現在同一行上。那麼你究竟希望他們如何放置?如果你想讓它們居中,你可以爲'TextViews'添加'android:layout_centerVertical =「true」'。 – Luksprog

+0

它們不在一行。和centerVertical不起作用 – darja

回答

0

不幸的是,唯一可行的解​​決方案是不使用LinearLayou t而不是嵌套RelativeLayout

6

選擇的TextView和GOTO性能

select Center Vertical for each textview

put android:layout_centerVertical="true" in xml

+0

我已經嘗試過,並得到這樣的東西:http://petromi.com/get/4ad0cd47d7.png – darja

+0

爲什麼你需要幾個嵌套relativelayouts?我認爲你可以沒有一個 – k0nig

+0

最初我有一個RelativeLayout。但我需要將幾個TextView對齊到底部,然後將該組居中。居中每個文本看起來不太好。 – darja

3

試試這個代碼:

<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" > 
< 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" > 

    <TextView 
     android:id="@+id/label" 
     style="@style/ConnectionPoint.Label" 
     android:layout_width="60dp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:text="Title" /> 

    <TextView 
     android:id="@+id/code" 
     style="@style/ConnectionPoint.Code" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:text="CODE" /> 

    <TextView 
     android:id="@+id/name" 
     style="@style/ConnectionPoint.Name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:text="Some text" /> 
    </RelativeLayout> 

    </RelativeLayout> 
+0

謝謝,但它沒有幫助 – darja

+0

你的圖片顯示左,右和中間標題,一些文本和代碼的順序.....上面的代碼以相同的方式顯示.. –

+0

也許我的問題不清楚。我希望他們按照底部對齊。 – darja

0

我知道這個問題是2歲,但我想我有一個更好的答案,如果任何人遇到此問題。它解決了我的問題,試圖在嵌套的相對佈局中對齊文本視圖。

假設OP代碼:

<RelativeLayout 
     android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true"> 
<TextView 
     android:layout_width="60dp" android:layout_height="wrap_content" 
     android:id="@+id/label" 
     style="@style/ConnectionPoint.Label" 
     android:text="Title"/> 
<TextView 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:id="@+id/code" 
     style="@style/ConnectionPoint.Code" 
     android:text="CODE"/> 
<TextView 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:id="@+id/name" 
     android:layout_toRightOf="@id/label" 
     android:layout_toLeftOf="@id/code" 
     style="@style/ConnectionPoint.Name" 
     android:text="Some text"/> 

下應努力對準由底部以下幾個要素:

<RelativeLayout 
      android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true"> 
     <TextView 
       android:layout_width="60dp" android:layout_height="wrap_content" 
       android:id="@+id/label" 
       android:text="Title"/> 
     <TextView 
       android:layout_width="wrap_content" android:layout_height="wrap_content" 
       android:id="@+id/code" 
       android:layout_alignBottom="@+id/label" 
       android:text="CODE"/> 
     <TextView 
       android:layout_width="wrap_content" android:layout_height="wrap_content" 
       android:id="@+id/name" 
       android:layout_toRightOf="@id/label" 
       android:layout_toLeftOf="@id/code" 
       android:layout_alignBottom="@+id/label" 
       android:text="Some text"/> 
    </RelativeLayout> 

這裏唯一的變化是增加以下行:

android:layout_alignBottom="@+id/{your-id-here}" 

您可能需要根據您的佈局對其進行調整 - 在上面的示例中,帶有id = label的TextView基本上是根,其他元素從它的底部開始。這就是爲什麼id = label沒有layout_alignBottom屬性。