2012-05-24 3 views
0

Должно быть очень просто решить, но я не могу это решить.Выравнивание раскладки Query {Center Horizontal + Center Vertical}

Я хочу выровнять «верхнее изображение и 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. – dira

+0

oops, удален слишком много lol, просто добавил его обратно, спасибо – 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_horizontal" android: layout_centerHorizontal = "true" android: layout_centerVertical = "true" с 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" /> 

Смежные вопросы