2012-01-16 128 views
0

我在使我的按钮文本在我的Android应用程序中正确对齐时遇到了一些麻烦。目前,我的按钮文本与按钮中心的文本左侧对齐,如图所示:中心右对齐按钮 - Android

|空白|文本在这里|

我曾尝试使用重力=“center_horizo​​ntal | center_vertical”无济于事。我已经尝试添加一个新的按钮到一个完全独立的项目,这仍然成立。它也在LinearLayouts和RelativeLayouts中做到这一点。

在此先感谢您的帮助。

这是一个布局文件。但是它在所有这些方面都做得很好,包括其他项目。此外,此布局上的所有按钮都有相同的问题。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<Button 
    android:layout_width="wrap_content" 
    android:text="@string/history_delete_record" 
    android:id="@+id/History_Delete_Record" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_gravity="center_horizontal|center_vertical"></Button> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/History_Delete_Record" 
    android:layout_alignLeft="@+id/History_Delete_Record" 
    android:id="@+id/History_Open_Details" 
    android:text="@string/history_open_record"></Button> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/History_Cancel" 
    android:text="@string/cancel" 
    android:layout_below="@+id/History_Open_Details" 
    android:layout_alignLeft="@+id/History_Open_Details" 
    android:layout_marginTop="19dp"> 
</Button> 
</RelativeLayout> 
+0

。 – cren90

+0

你可以发布相关的布局XML吗? –

+0

是的,请添加您的布局文件。 – Orest

回答

0

下面是一个简单的执行按钮以及居中的文本:

<Button xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/myBtn" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal|center_vertical" 
    android:text="Button text!" /> 

与您的代码的问题是,您正在设置宽度以像素为单位时,你应该使用XML属性android:layout_width="wrap_content" ,它将包装按钮文本以适应相应的视图。

另请注意,您不应该以像素为单位设置视图的宽度,因为这在跨越不同的屏幕尺寸和密度时效果不佳。不要使用“px”,而应在“dp”中指定宽度,这是一个与密度无关的单位值。有关此主题的更多信息,请参见post

编辑 这里是你用你的布局我建议的XML代码:我会包括一个按钮的PIC但SO不会让我张贴图片还

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/History_Delete_Record" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:text="@string/history_delete_record" > 
    </Button> 

    <Button 
     android:id="@+id/History_Open_Details" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/History_Delete_Record" 
     android:layout_alignRight="@+id/History_Delete_Record" 
     android:layout_below="@+id/History_Delete_Record" 
     android:text="@string/history_open_record" > 
    </Button> 

    <Button 
     android:id="@+id/History_Cancel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/History_Open_Details" 
     android:layout_alignRight="@+id/History_Open_Details" 
     android:layout_below="@+id/History_Open_Details" 
     android:text="@string/cancel" > 
    </Button> 

</RelativeLayout> 
+0

也注意到'android:gravity ='center_horizo​​ntal | center_vertical''这一行在这里没有必要。如果我们将其取出,默认情况下,文本将在视图的中心绘制。 –

+0

我已经删除了宽度值,如你所建议的,但问题仍然存在。此外,现在按钮不能是相同的大小。 – cren90

+0

哎呀,没有意识到你想要的按钮是相同的大小。在修改xml代码时保持一秒。 –