2013-07-31 61 views
1
垂直居中

我的活動只有一個TextView的,我試圖讓文本垂直居中,但它最終對準底部。我嘗試改變幾個屬性,但沒有運氣。這是目前我有什麼(安卓2.3):的TextView不會在父母

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

    <TextView 
     android:id="@+id/tvFlash" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="#ff0000" 
     android:padding="0dp" 
     android:textStyle="bold" 
     android:includeFontPadding="false" 
     android:gravity="center_vertical" 
     android:layout_centerInParent="true" /> 

</RelativeLayout> 

編輯: 我實際上設定代碼TEXTSIZE,這是一個較大的值。如果TEXTSIZE增大到屏幕(在橫向模式)的寬度,然後將其對準出現在屏幕的底部。但是,如果使用正常的文本大小(如20sp),它將顯示居中。這告訴我,當你增加TEXTSIZE是非常大的,看來Android是增加內填充一些隱藏的(雖然我沒設置includeFontPadding爲false)。

+0

我試過你的代碼,它看起來很好。 – Goofy

+0

如果這是你整個佈局那麼它的正確定位我的結束。 –

+0

嘗試Layout_gravity代替重力。並刪除layout_centerinparent。 –

回答

2

看起來不錯,但是,

你爲什麼不使用;

android:layout_centerVertical="true" 

代替

android:gravity="center_vertical" 
android:layout_centerInParent="true" 
0

即使沒有佈局:重力,它看起來還好我。

android:layout_centerInParent="true" 

android:layout_centerVertical="true" 
android:layout_centerHorizontal="true" 

你也可以修改你的親戚佈局

android:layout_width="match_parent" 
android:layout_height="match_parent" 

由於match_parent是更明智的API 8

開始這是唯一的佈局活動?或者這是片段的一部分?