2016-04-20 44 views
1

我试图将我的相对布局的chilldren居中在我的屏幕中央,但它的行为就像它对齐到父级的顶部,我无法弄清楚为什么。相对布局重心没有正确对齐

我.XML:

<?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" 
    android:layout_gravity="center" 
    android:gravity="center" 
    android:background="#f70909"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageView2" 
     android:adjustViewBounds="true" 
     android:src="@drawable/dice"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:id="@+id/textMessage" 
     android:layout_centerHorizontal="true" 
     android:layout_below="@+id/imageView2" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/editUserSplash" 
     android:hint="Username" 
     android:gravity="center" 
     android:layout_centerHorizontal="true" 
     android:layout_below="@+id/textMessage" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/editPasswordSplash" 
     android:hint="Password" 
     android:layout_centerHorizontal="true" 
     android:gravity="center" 
     android:layout_below="@+id/editUserSplash" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Login" 
     android:id="@+id/btnSplash" 
     android:layout_centerHorizontal="true" 
     android:layout_below="@+id/editPasswordSplash" /> 

    <ProgressBar 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/prgSplash" 
     style="@android:style/Widget.DeviceDefault.ProgressBar.Large" 
     android:layout_centerHorizontal="true" 
     android:indeterminate="true" 
     android:layout_below="@+id/btnSplash" /> 

</RelativeLayout> 

我试图使父relativelayout没有成功,也不会对齐到底层无论是。最初我以为布局并没有填满整个屏幕,但由于其高度和宽度是match_parent我不认为这是问题。在Android工作室它显示正确,但我必须错过一些小东西。 screenshot我需要的意见留相对于彼此相同的形成中,但在屏幕垂直方向上居中:

我也使用gravitylayoutgravity参数和两者的组合,但没有成功

编辑试过。

编辑2:我将RelativeLayout的背景设置为红色,并得到了这个:red background因此,relativelayout不填充我的屏幕。

编辑3:archi rayan 编辑4:archi rayan

+0

use android:layout_centerInParent =“true” – USKMobility

+0

你是指父母还是孩子? – dec0yable

+0

你想要什么? –

回答

1

由于ANK我能弄清楚,为什么RelativeLayout的没有填写我的屏幕的原因是,我用它在alertDialog中。所以RelativeLayouts父类不是屏幕而是alertDialog。由于alertDialog包装其内容,因此不会填满整个屏幕。

1

尝试这样的事情。

<?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"> 


     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/imageView2" 
      android:adjustViewBounds="true" 
      android:layout_centerInParent="true"/> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:gravity="center" 
     android:layout_centerInParent="true"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:id="@+id/textMessage" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/editUserSplash" 
      android:hint="Username" 
      android:gravity="center" /> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/editPasswordSplash" 
      android:hint="Password" 
      android:gravity="center" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Login" 
      android:id="@+id/btnSplash" /> 

     <ProgressBar 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/prgSplash" 
      style="@android:style/Widget.DeviceDefault.ProgressBar.Large" 
      android:indeterminate="true" /> 

    </LinearLayout> 

</RelativeLayout> 

UPDATE:

如果设置在AlertDialog的XML,它可能是有在底部分配的空间。我谷歌,发现这alert_dialog.xml作为参考。看起来底部有一个按钮面板,最低高度为54dip。

<LinearLayout android:id="@+id/buttonPanel" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="54dip" 
    android:orientation="vertical" > 
    <LinearLayout 
     style="?android:attr/buttonBarStyle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:paddingTop="4dip" 
     android:paddingStart="2dip" 
     android:paddingEnd="2dip" 
     android:measureWithLargestChild="true"> 
     <LinearLayout android:id="@+id/leftSpacer" 
      android:layout_weight="0.25" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:visibility="gone" /> 
     <Button android:id="@+id/button1" 
      android:layout_width="0dip" 
      android:layout_gravity="start" 
      android:layout_weight="1" 
      style="?android:attr/buttonBarButtonStyle" 
      android:maxLines="2" 
      android:layout_height="wrap_content" /> 
     <Button android:id="@+id/button3" 
      android:layout_width="0dip" 
      android:layout_gravity="center_horizontal" 
      android:layout_weight="1" 
      style="?android:attr/buttonBarButtonStyle" 
      android:maxLines="2" 
      android:layout_height="wrap_content" /> 
     <Button android:id="@+id/button2" 
      android:layout_width="0dip" 
      android:layout_gravity="end" 
      android:layout_weight="1" 
      style="?android:attr/buttonBarButtonStyle" 
      android:maxLines="2" 
      android:layout_height="wrap_content" /> 
     <LinearLayout android:id="@+id/rightSpacer" 
      android:layout_width="0dip" 
      android:layout_weight="0.25" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:visibility="gone" /> 
    </LinearLayout> 
</LinearLayout> 
+0

运气不好的图片仍然与屏幕顶端的 – dec0yable

+0

更新了代码。你现在可以尝试吗? – ank

+0

现在的意见是在图像中居中,所以相关布局不会填满我的整个屏幕出于某种原因... – dec0yable

1
I think this may help you 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_height="match_parent" 
android:layout_width="match_parent" 
android:gravity="center"> 
    <RelativeLayout 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Login" 
     android:textStyle="bold" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:id="@+id/textMessage" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" /> 
    <EditText 
     android:layout_margin="10dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/editUserSplash" 
     android:hint="Username" 
     android:layout_below="@+id/textMessage" 
     android:gravity="center" /> 
    <EditText 
     android:layout_margin="10dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/editPasswordSplash" 
     android:hint="Password" 
     android:layout_below="@+id/editUserSplash" 
     android:gravity="center" /> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Login" 
     android:id="@+id/btnSplash" 
     android:layout_below="@+id/editPasswordSplash" 
     android:layout_centerHorizontal="true" /> 
</RelativeLayout> 

+0

同样的问题,当我这样做的布局只是变得像图像一样大。它将外部布局视为'wrap_content'而不是'match_parent' – dec0yable

+0

您可以向我发送截图吗? – Krupa

+0

新增光阑 – dec0yable

3

试试这个:

<?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:background="#f70909"> 

<RelativeLayout 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_centerInParent="true"> 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/imageView2" 
    android:adjustViewBounds="true" 
    android:src="@drawable/dice"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:id="@+id/textMessage" 
    android:layout_centerHorizontal="true" 
    android:layout_below="@+id/imageView2" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/editUserSplash" 
    android:hint="Username" 
    android:gravity="center" 
    android:layout_centerHorizontal="true" 
    android:layout_below="@+id/textMessage" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/editPasswordSplash" 
    android:hint="Password" 
    android:layout_centerHorizontal="true" 
    android:gravity="center" 
    android:layout_below="@+id/editUserSplash" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Login" 
    android:id="@+id/btnSplash" 
    android:layout_centerHorizontal="true" 
    android:layout_below="@+id/editPasswordSplash" /> 

<ProgressBar 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/prgSplash" 
    style="@android:style/Widget.DeviceDefault.ProgressBar.Large" 
    android:layout_centerHorizontal="true" 
    android:indeterminate="true" 
    android:layout_below="@+id/btnSplash" /> 

</RelativeLayout> 
</RelativeLayout>