2010-03-16 32 views
6

我正在使用Android 1.5中的列表視圖來顯示圖像旁邊的圖像和文本列表。我正在嘗試垂直居中文本,但文本位於行的頂部而不是居中。下面是我的佈局:如何使用相對佈局在列表中垂直對齊項目?

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/row" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="10dip"> 

    <ImageView android:id="@+id/item_image" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:paddingRight="10dip" 
     android:src="@drawable/default_image" android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentBottom="true" 
     android:layout_centerVertical="true" 
     android:gravity="center_vertical"/> 

    <TextView android:id="@+id/item_title" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/item_image" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentBottom="true" 
     android:layout_centerVertical="true" 
     android:gravity="center_vertical" 
     /> 

</RelativeLayout> 

這似乎很奇怪,我需要設置alignParentTop =「真」當我試圖垂直居中的文本,但如果我不文本甚至不露面。我究竟做錯了什麼?

回答

16

編輯以下的評論:

原來製作與RelativeLayout的這項工作是不容易的。在答案的底部,我包含了一個RelativeLayout,它提供了所需的效果,但是直到它包含在ListView中。之後,發生了與問題中所述相同的問題。這是通過使用LinearLayout(s)來修復的。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingLeft="10dp" 
    android:orientation="horizontal"> 

    <ImageView android:id="@+id/pickImageImage" 
     android:layout_width="100dp" 
     android:layout_height="80dp" 
     android:background="@drawable/icon" 
     android:scaleType="fitXY" 
     android:layout_marginRight="10dp"/> 

    <TextView android:id="@+id/pickImageText" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     android:gravity="left|center_vertical" 
     android:text="I'm the text"/> 

</LinearLayout> 

如果你想有兩個文本框,你可以嵌套第二orientation="vertical"和LinearLayout中的ImageView的之後,然後把文本框在裏面。

這有效,但我不得不承認,我不知道爲什麼RelativeLayouts沒有。例如,這個blog post by Romain Guy明確指出RelativeLayout應該。當我嘗試它時,我從來沒有完成它的工作;誠然,我並沒有像他那樣做,但我唯一的變化是TextViews的一些屬性,而這些屬性應該沒有什麼不同。


這裏是原來的答案:

我想你在RelativeLayout的所有這些有點矛盾的指示混亂的Android。我把你的東西重新格式化爲:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/row" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="10dip"> 

    <ImageView android:id="@+id/item_image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingRight="10dip" 
     android:src="@drawable/icon" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true"/> 

    <TextView android:id="@+id/item_title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/item_image" 
     android:layout_centerVertical="true" 
     android:text="Blah!"/> 

</RelativeLayout> 

而且工作正常。我刪除了許多冗餘android:layout_alignParentxxx,因爲它們不是必需的。這個視圖現在出現在左上角的圖片和垂直居中的文本旁邊。如果你希望圖片垂直居中,那麼你不能在android:layout_height =「wrap_content」上使用RelativeLayout,因爲它試圖讓自身不比圖片的高度更高。您必須指定高度,例如80dp,然後將ImageView設置爲一個固定的高度,例如60dp,並使用android:scaleType =「fitXY」將其縮小以適合正確的尺寸。

+0

感謝您的嘗試。我確實希望佈局不會高於圖像,所以圖像很好。我嘗試了你的xml,並且看到我用我的方式看到的同樣的東西。除非我向TextView添加android:layout_alignParentTop =「true」,否則文本不會顯示出來,然後當然是頂端對齊而不是居中。你把你的ListView放在裏面嗎? – 2010-03-16 13:05:32

+0

嗯。當它是一個獨立的視圖時它工作得很好,但是當我將它添加到一個ListView然後是時,它停止工作。我剛剛嘗試了一些東西,沒有什麼能夠很好地解決它應該使用RelativeLayout的方式。儘管你可以使用LinearLayout來達到同樣的效果。將方向設置爲水平,然後使TextView高度變爲fill_parent,並使用center_vertical重力。如果你想在圖標旁邊有幾個TextView,那麼你可以把它們放在第二個垂直的LinearLayout中。這不是優雅的,但我不能解決爲什麼RelativeLayouts不工作... – 2010-03-16 14:17:13

+0

你可以編輯你的答案提及LinearLayout?這似乎工作。 – 2010-03-17 01:26:07

0

基線指令可以做到這一點,但ImageView現在根本不支持基線對齊。您可以通過創建ImageView的子類來解決此問題,重寫getBaseline()方法並返回圖像的高度。

+0

謝謝,今晚我會試試看看它是否有效。必須對ImageView進行子類化似乎很痛苦,但我會嘗試一下。 – 2010-03-16 14:29:39

1

被困在了一陣類似的問題,但是發現這個從CommonsWare:

「當你膨脹的佈局,使用inflate(R.layout.whatever, parent, false),其中父母是ListView的。」

工程,但只有當你設置行的高度爲一個特定的值(即你不能使用wrap_content)。