2012-05-24 130 views
0

必須非常簡單才能解決,但我無法解決此問題。版面對齊查詢{中心水平+中心垂直}

我想對齊「頂部圖像和log_in_box」,所以整個事情出現在屏幕的中心(水平+垂直中心)。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:orientation="vertical"> 

    <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" 
     android:layout_gravity="center_vertical|center_horizontal"> 

     <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" 
      android:contentDescription="" /> 

     <LinearLayout android:id="@+id/log_in_box" android:layout_width="match_parent" android:layout_height="wrap_content" 
      android:orientation="vertical"> 
      <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="User ID" 
       android:inputType="text" /> 

      <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" 
       android:hint="Password" android:inputType="textPassword" /> 

      <CheckBox android:layout_width="match_parent" android:layout_height="wrap_content" android:checked="true" 
       android:text="Remember Me" android:layout_marginTop="10dp" /> 

      <Button android:id="@+id/log_in" android:layout_width="match_parent" android:layout_height="wrap_content" 
       android:layout_marginTop="10dp" android:text="Log In" /> 
     </LinearLayout> 

    </LinearLayout> 

</LinearLayout> 

目前看 enter image description here

回答

1
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

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

     <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" 
      android:contentDescription="" /> 

     <LinearLayout android:id="@+id/log_in_box" android:layout_width="match_parent" android:layout_height="wrap_content" 
      android:orientation="vertical"> 
      <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="User ID" 
       android:inputType="text" /> 

      <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" 
       android:hint="Password" android:inputType="textPassword" /> 

      <CheckBox android:layout_width="match_parent" android:layout_height="wrap_content" android:checked="true" 
       android:text="Remember Me" android:layout_marginTop="10dp" /> 

      <Button android:id="@+id/log_in" android:layout_width="match_parent" android:layout_height="wrap_content" 
       android:layout_marginTop="10dp" android:text="Log In" /> 
     </LinearLayout> 

    </LinearLayout> 

</RelativeLayout> 
+0

+1您的回答是正確的,但外部(第一個)LinearLayout需要android:orientation =「vertical」。 – dira

+0

哎呀,刪除太多哈哈,只是加回來,謝謝 – candyleung

1

試試這個。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:orientation="vertical"> 

<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_gravity="center_horizontal" 
    android:layout_centerHorizontal="true" 
android:layout_centerVertical="true"> 

    <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" 
     android:contentDescription="" /> 

    <LinearLayout android:id="@+id/log_in_box" android:layout_width="match_parent" android:layout_height="wrap_content" 
     android:orientation="vertical"> 
     <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="User ID" 
      android:inputType="text" /> 

     <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" 
      android:hint="Password" android:inputType="textPassword" /> 

     <CheckBox android:layout_width="match_parent" android:layout_height="wrap_content" android:checked="true" 
      android:text="Remember Me" android:layout_marginTop="10dp" /> 

     <Button android:id="@+id/log_in" android:layout_width="match_parent" android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" android:text="Log In" /> 
    </LinearLayout> 

</LinearLayout> 

</RelativeLayout> 
+0

+1感謝。你的答案也是正確的,但android:layout_gravity =「center_horizo​​ntal」 android:layout_centerHorizo​​ntal =「true」 android:layout_centerVertical =「true」with android:layout_centerInParent =「true」。 – dira

0

<ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:contentDescription="" 
     android:src="@drawable/ic_launcher" /> 



     <EditText 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:hint="User ID" 
      android:inputType="text" /> 

     <EditText 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:hint="Password" 
      android:inputType="textPassword" /> 

     <CheckBox 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:checked="true" 
      android:text="Remember Me" /> 

     <Button 
      android:id="@+id/log_in" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:text="Log In" />