【问题标题】:Layout alignment Query {Center Horizontal + Center Vertical}布局对齐查询{水平居中+垂直居中}
【发布时间】:2012-05-30 17:35:37
【问题描述】:

一定很容易解决,但我无法解决。

我想对齐“顶部图像和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>

当前外观

【问题讨论】:

    标签: android android-layout android-linearlayout


    【解决方案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>
    

    【讨论】:

    • +1 您的答案是正确的,但外部(第一个)LinearLayout 需要 android:orientation="vertical"。
    【解决方案2】:

    试试这个。

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

    【讨论】:

    • +1 谢谢。您的回答也是正确的,但将 android:layout_gravity="center_horizo​​ntal" android:layout_centerHorizo​​ntal="true" android:layout_centerVertical="true" 替换为 android:layout_centerInParent="true"。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-15
      • 1970-01-01
      • 1970-01-01
      • 2016-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多