1

我遇到了TextViews問題,並將文本居中在文本區域內。具體來說,我有一個TextView,但是當我的文本里麪包含跨越底部邊距的字母(即p,g,q,y等)時,這些字母就會被截斷。我試圖將文字置於該區域內,但沒有多少運氣。在使用TextView在文本區域中居中文本時遇到問題

[已更新]我現在使用wrap_content作爲我的高度解決了在底部截斷的字母,但發現了另一個問題。現在看來,該文本在該地區的定位較低,這使得這一差距處於頂部。我修改了我的佈局以反映最新(見下文)。基本上,那些在(g,y,j等)之前被截斷的角色正在接觸右下方的區域,這很好,但它似乎在頂部留下了填充。我試圖改變重力center_vertical或中心,但沒有多少運氣:

注意,我有鑑於textSizes的規範工作(即我不能改變這些值)

<LinearLayout 
    android:id="@+id/header" 
    android:layout_width="fill_parent" 
    android:layout_height="43.3dip" 
    android:background="@drawable/custom_bg" 
    android:orientation="horizontal"> 
    <ImageButton 
     android:id="@+id/headshot" 
     android:layout_width="43.3dip" 
     android:layout_height="43.3dip" 
     android:src="@drawable/sample" 
     android:background="@drawable/head_btn" 
     android:layout_gravity="center" /> 
    <RelativeLayout 
     android:id="@+id/name_and_email" 
     android:layout_width="230.7dip" 
     android:layout_height="43.3dip" 
     android:orientation="vertical"> 


    <TextView 
     android:id="@+id/name" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textColor="@color/custom_color" 
     android:textSize="18.6sp" 
     android:singleLine="true" 
     android:ellipsize="end" 
     android:layout_alignParentTop="true" 
     android:gravity="center_vertical" 
     android:background="#c5ff15" 
     android:lineSpacingExtra="0sp" 
     android:text="AaBbCcDdGgJjTtYy" /> 
    <TextView 
     android:id="@+id/email" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textColor="@color/custom_color" 
     android:textSize="13.3sp" 
     android:singleLine="true" 
     android:ellipsize="end" 
     android:layout_centerInParent="true" 
     android:layout_below="@id/name" 
     android:gravity="center_vertical" 
     android:visibility="gone" 
     android:text="[email protected]" /> 


    </RelativeLayout> 
    <ImageButton 
     android:id="@+id/headshot2" 
     android:layout_width="43.3dip" 
     android:layout_height="43.3dip" 
     android:src="@drawable/sample" 
     android:background="@drawable/head_btn2" 
     android:layout_gravity="center" /> 
</LinearLayout> 

誰能幫助?

MB

回答

1

爲什麼你要手動設置高度?使用layout_height =「wrap_content」你會有更好的運氣。我想象一下,問題是你的文本大小14.3sp對於你允許的區域來說太大了。

另外,調試複雜佈局的好資源是工具文件夾下的HeirarchyViewer。

+0

感謝您的回覆。我現在實際上已經將它移到了wrap_content,並且確實解決了我擁有的文本下降問題。但是現在我又遇到了另一個問題:文本本身看起來像是向下移動到底部,在我的區域頂部留下這個「間隙」或填充。我試圖使用center_vertical將其居中,但它不起作用。我修改了上面兩個TextView的佈局,使高度現在爲wrap_content,android:gravity爲「center_vertical」,但我仍然有一些問題(g和p現在適合,但它看起來有一個缺口在頂部) – mbethdev 2010-08-06 18:27:14

+0

您應該可以使用更新後的xml編輯原始問題。很難說現在有什麼,但我會嘗試使用heirarchy查看器來查看究竟是誰負責多餘的空間。那麼你應該能夠在該元素上調整填充等。 – 2010-08-06 19:07:59

+0

好的,我修改了它們的佈局以簡化它(所以如果你確實有機會在Eclipse上加載佈局,你應該只能看到頂部的textview)。據我所知,填充發生在*文本區域內,它應該能夠在TextView內控制,但重力似乎不起作用。我必須錯過另一個屬性,但不知道它是什麼:( – mbethdev 2010-08-06 20:12:03