2010-09-28 48 views
2

我需要创建固定大小的sqare按钮,并在其上为计算器应用程序提供相对较大的字符。 当我增加文字大小时,角色不再显示在按钮的中心,按钮的位置将一些像素移到顶部(非常奇怪)。如何在Android 2.2中创建带大文本的小按钮?

http://img9.imageshack.us/i/buttontest.png/

如果有可能,我不想使用图像的按钮。

更新:

我用这个XML:

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/TableLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"> 
<TableRow> 
<Button android:layout_width="50dp" android:layout_height="50dp" android:text="1" android:textSize="8dp"></Button> 
<Button android:layout_width="50dp" android:layout_height="50dp" android:text="2" android:textSize="10dp"></Button> 
<Button android:layout_width="50dp" android:layout_height="50dp" android:text="3" android:textSize="12dp"></Button> 
<Button android:layout_width="50dp" android:layout_height="50dp" android:text="4" android:textSize="14dp"></Button> 
<Button android:layout_width="50dp" android:layout_height="50dp" android:text="5" android:textSize="16dp"></Button> 
<Button android:layout_width="50dp" android:layout_height="50dp" android:text="6" android:textSize="18dp"></Button> 
</TableRow><TableRow> 
<Button android:layout_width="50dp" android:layout_height="50dp" android:text="7" android:textSize="20dp"></Button> 
<Button android:layout_width="50dp" android:layout_height="50dp" android:text="8" android:textSize="22dp"></Button> 
<Button android:layout_width="50dp" android:layout_height="50dp" android:text="9" android:textSize="24dp"></Button> 
<Button android:layout_width="50dp" android:layout_height="50dp" android:text="A" android:textSize="26dp"></Button> 
<Button android:layout_width="50dp" android:layout_height="50dp" android:text="B" android:textSize="28dp"></Button> 
<Button android:layout_width="50dp" android:layout_height="50dp" android:text="C" android:textSize="30dp"></Button> 
</TableRow> 
<TableRow></TableRow> 
</TableLayout> 

而得到这样的结果:

alt text

我该如何的所有按钮equallly排列居中文本?

回答

3
![<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="50dp" > 
     <Button android:layout_width="50dp" android:layout_height="fill_parent" android:text="1" android:textSize="8dp"></Button> 
     <Button android:layout_width="50dp" android:layout_height="fill_parent" android:text="2" android:textSize="10dp"></Button> 
     <Button android:layout_width="50dp" android:layout_height="fill_parent" android:text="3" android:textSize="12dp"></Button> 
     <Button android:layout_width="50dp" android:layout_height="fill_parent" android:text="4" android:textSize="14dp"></Button> 
     <Button android:layout_width="50dp" android:layout_height="fill_parent" android:text="5" android:textSize="16dp"></Button> 
     <Button android:layout_width="50dp" android:layout_height="fill_parent" android:text="6" android:textSize="18dp"></Button> 
    </LinearLayout> 


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="50dp"> 

     <Button android:layout_width="50dp" android:layout_height="fill_parent" android:text="7" android:textSize="20dp"></Button> 
     <Button android:layout_width="50dp" android:layout_height="fill_parent" android:text="8" android:textSize="22dp"></Button> 
     <Button android:layout_width="50dp" android:layout_height="fill_parent" android:text="9" android:textSize="24dp"></Button> 
     <Button android:layout_width="50dp" android:layout_height="fill_parent" android:text="A" android:textSize="26dp"></Button> 
     <Button android:layout_width="50dp" android:layout_height="fill_parent" android:text="B" android:textSize="28dp"></Button> 
     <Button android:layout_width="50dp" android:layout_height="fill_parent" android:text="C" android:textSize="30dp"></Button> 
    </LinearLayout> 

</LinearLayout>] 

alt text

+0

谢谢,这是有效的。我在另一种方法中找到了一个方法:在TableRow标签中设置android:baselineAligned =“false”的方式类似。 – elsni 2010-09-29 09:53:59

5

使用android:paddingTop="2dp"

代码:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="50dp" > 

    <Button 
    android:layout_width="wrap_content" 
    android:layout_height ="fill_parent" 
    android:text="padding = 1dp" 
    android:textSize="30sp" 
    android:paddingTop="1dp" /> 

    <Button 
    android:layout_width="wrap_content" 
    android:layout_height ="fill_parent" 
    android:textSize="30sp"  
    android:text="padding = 10dp" 
    android:paddingTop="10dp" />   
</LinearLayout> 

结果:

alt text

+1

会影响布局,但如果按钮排列在表格布局中,则不会给出正确的结果。即使对于文本较小的按钮,我也尝试了paddingTop的几个值。 – elsni 2010-09-28 10:23:04

+0

+0

这种行为对我来说完全不合逻辑。 paddingTop会影响按钮本身的定位,而不会影响按钮本身的定位。在我看来,就像布局系统中的一个bug。 – elsni 2010-09-28 12:17:51

2

我发现在视图中添加android:includeFontPadding =“false”解决了超大文本问题。

相关问题