2016-04-20 46 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>