2015-11-06 16 views
0

我有一個問題,試圖做一些非常簡單的事情:在具有固定寬度和高度的按鈕/ TextView中居中文本,我的文本保持推下來,我增加它的大小,這裏是我使用的代碼示例:Android:文本沒有被集中在固定寬度/高度的按鈕/文本視圖中

<Button 
      android:layout_width="70dp" 
      android:layout_height="70dp" 
      android:background="@drawable/circle_add_item" 
      android:gravity="center" 
      android:text="+" 
      android:textColor="@color/green" 
      android:textSize="75sp" /> 

這裏有一個像什麼,我得到的結果:

enter image description here

會有人幫幫我 ?

謝謝!

回答

0

嘗試添加填充頂部,0dp

android:paddingTop="0dp" 
+0

試過這樣做,但它不工作 –

+0

請嘗試將textSize減小到55sp –

+0

似乎完全集中,謝謝!我想知道,如果有另一種方法來解決它 –

1

此外,您還可以使用這種方式

<RelativeLayout 
    android:layout_width="70dp" 
    android:layout_height="70dp" 
    android:background="@drawable/circle"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="75sp" 
     android:layout_marginTop="-20dp" 
     android:background="@android:color/transparent" 
     android:text="+" 
     android:textColor="#00ff00" 
     android:paddingTop="0dp" 
     android:layout_centerVertical="true"/> 

</RelativeLayout> 

enter image description here

+0

是的,我看到了,正如你所說這不是「優雅」,另一種解決方案似乎更簡單x) –

+0

但爲什麼你不使用圖標? –

+0

圖標是一個圖像,加載它會比簡單的文本花費更多的時間 –

相關問題