2015-07-10 75 views
0

我试图让按钮位于屏幕中心和底部的注销按钮。我有垂直居中的按钮,但它不会水平居中。带有自定义按钮的Android布局

我做错了什么?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:padding="10dp" 
    android:weightSum="1"> 

<TextView 
    android:layout_width="match_parent" 
    android:textColor="#000000" 
    android:id="@+id/etEmailLabel" 
    android:layout_height="wrap_content" 
    android:textSize="20sp" 
    android:gravity="center" 
    android:textAlignment="center" /> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="150dp" 
    android:layout_marginBottom="20dp" 
    android:weightSum="1" 
    android:layout_weight="0.98"> 

    <Button 
     android:layout_width="291dp" 
     android:layout_height="273dp" 
     android:id="@+id/DataLog" 
     android:background="@layout/mybutton" 
     android:text="Log Location" 
     android:layout_gravity="center" /> 

</LinearLayout> 

<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/bLogout" 
    android:text="Logout"/> 

</LinearLayout> 

自定义按钮代码:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="50dp" 
    android:layout_width="50dp" 
    android:layout_gravity="center" 
    android:shape="oval"> 
    <solid android:color="#FFFFFF"/> 
    <stroke android:width="2sp" android:color="#fff" /> 
</shape> 
+0

尝试使用相对布局 – HassanUsman

+0

有趣的是,删除了所有居中。 – Walorn

+0

因为首先你使用线性布局现在拖放按钮在你想要的地方 – HassanUsman

回答

0

我很困惑,为什么你LinearLayout高度比按钮较小,但您可以通过应用重力父ViewGroup居中:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="150dp" 
    android:layout_marginBottom="20dp" 
    android:weightSum="1" 
    android:gravity="center" 
    android:layout_weight="0.98"> 

    <Button 
     android:layout_width="291dp" 
     android:layout_height="273dp" 
     android:id="@+id/DataLog" 
     android:background="@layout/mybutton" 
     android:text="Log Location" /> 

</LinearLayout> 

此外,您的LinearLayout的权重和weightSum不会有任何影响。

+0

好吧,所以我想出了你的感谢。我不得不居中主线性布局。不完全确定为什么会是这种情况,但有趣的事情就更少了。谢谢! – Walorn

+1

NP,其余的布局有很多东西没有意义,但我建议更多地了解ViewGroups。 –