2010-07-09 176 views
3

在下面的圖片中,我想紅色textview是垂直居中,我想繼續使用RelativeLayout沒有任何LinearLayouts,我想我可以找出如何使用LinearLayout做到這一點,但做不想使用這些。在RelativeLayout中垂直居中TextView

這是所有基於關閉的例子,我發現這裏android devleper's blog - Android Layout Tricks #1

alt text http://i29.tinypic.com/2rmku52.jpg

這裏是我的xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight" 
    android:padding="6dip"> 

    <ImageView android:id="@+id/icon" android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:src="@drawable/chevron" /> 



    <TextView android:id="@+id/rightText" android:layout_width="55px" 
     android:layout_height="fill_parent" android:layout_toLeftOf="@id/icon" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentBottom="true" 
     android:textSize="14dip" 
     android:text="$15.99" /> 


    <TextView android:id="@+id/secondLine" android:layout_width="wrap_content" 
     android:layout_height="26dip" android:layout_toLeftOf="@id/rightText" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:text="Simple application that shows how to use RelativeLayout" /> 

    <TextView android:layout_width="fill_parent" android:id="@+id/firstLine" 
     android:layout_height="wrap_content" android:layout_toLeftOf="@id/rightText" 
     android:layout_alignParentLeft="true" android:layout_alignParentTop="true" 
     android:layout_above="@id/secondLine" 
     android:layout_alignWithParentIfMissing="true" android:gravity="center_vertical" 
     android:singleLine="true" 
     android:ellipsize="end" 
     android:maxLines="1" 
     android:textSize="13dip" 
     android:text="My Application that has a really long title asdfsdf asdf sdf asdf" /> 

</RelativeLayout> 

回答

6

你只需要設置TextView的嚴重性。

+0

非常感謝,這樣做的伎倆 – slim 2010-07-10 00:54:25

2

如果上面的答案不起作用,可能會對其他人有幫助。只需在textView中設置下面的屬性

android:layout_centerVertical="true"