2015-11-06 36 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

图标是一个图像,加载它会比简单的文本花费更多的时间 –

相关问题