在 Android 中对齐两个 LinearLayouts 不起作用
Align two LinearLayouts in Android not working
我正在尝试在 RelativeLayout
中对齐两个 LinearLayouts
。我希望第一个在 RelativeLayout
的中心对齐,第二个在底部对齐。
这是我的代码:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:gravity="center" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/image_descritpion"
android:src="@drawable/logo_white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/healthfile_title"
android:textColor="#ffffff"
android:textSize="22sp" />
<View
android:layout_width="160dp"
android:layout_height="1px"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="#ffffff" />
<EditText
android:id="@+id/text_userName"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textColorHint="#cdcdcd"
android:background="@drawable/editbox_style"
android:padding="10dp"
android:layout_marginTop="5dp"
android:gravity="center"
android:hint="@string/home_email" />
<EditText
android:id="@+id/text_userPassword"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textColorHint="#cdcdcd"
android:background="@drawable/editbox_style"
android:padding="10dp"
android:inputType="textPassword"
android:layout_marginTop="5dp"
android:gravity="center"
android:hint="@string/home_password" />
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginTop="5dp"
android:background="@drawable/button_style"
android:text="@string/home_login"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Don't have an account already?" />
</LinearLayout>
</RelativeLayout>
但是当我在 RelativeLayout
内对齐 hese LinearLayouts
时,第一个保持在顶部对齐...
我做错了什么?
尝试将这两个属性添加到您的第一个 LinearLayout
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
您需要在中心布局中添加 layout_centerInParent="true"
并从 RelativeLayout
中删除 android:gravity="center"
:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_centerInParent="true">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="s" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="22sp" />
<View
android:layout_width="160dp"
android:layout_height="1px"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="#ffffff" />
<EditText
android:id="@+id/text_userName"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textColorHint="#cdcdcd"
android:padding="10dp"
android:layout_marginTop="5dp"
android:gravity="center" />
<EditText
android:id="@+id/text_userPassword"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textColorHint="#cdcdcd"
android:padding="10dp"
android:inputType="textPassword"
android:layout_marginTop="5dp"
android:gravity="center" />
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginTop="5dp"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Don't have an account already?" />
</LinearLayout>
</RelativeLayout>
我已经删除了背景和字符串以便在我的电脑中预览。
您需要为第一个布局分配一个 id:
android:id="@+id/first" // insert this code into the first layout
您应该将第二个布局移到第一个布局下方:
android:layout_below="@id/first" // write this code into the second layout
我正在尝试在 RelativeLayout
中对齐两个 LinearLayouts
。我希望第一个在 RelativeLayout
的中心对齐,第二个在底部对齐。
这是我的代码:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:gravity="center" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/image_descritpion"
android:src="@drawable/logo_white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/healthfile_title"
android:textColor="#ffffff"
android:textSize="22sp" />
<View
android:layout_width="160dp"
android:layout_height="1px"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="#ffffff" />
<EditText
android:id="@+id/text_userName"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textColorHint="#cdcdcd"
android:background="@drawable/editbox_style"
android:padding="10dp"
android:layout_marginTop="5dp"
android:gravity="center"
android:hint="@string/home_email" />
<EditText
android:id="@+id/text_userPassword"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textColorHint="#cdcdcd"
android:background="@drawable/editbox_style"
android:padding="10dp"
android:inputType="textPassword"
android:layout_marginTop="5dp"
android:gravity="center"
android:hint="@string/home_password" />
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginTop="5dp"
android:background="@drawable/button_style"
android:text="@string/home_login"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Don't have an account already?" />
</LinearLayout>
</RelativeLayout>
但是当我在 RelativeLayout
内对齐 hese LinearLayouts
时,第一个保持在顶部对齐...
我做错了什么?
尝试将这两个属性添加到您的第一个 LinearLayout
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
您需要在中心布局中添加 layout_centerInParent="true"
并从 RelativeLayout
中删除 android:gravity="center"
:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_centerInParent="true">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="s" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="22sp" />
<View
android:layout_width="160dp"
android:layout_height="1px"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="#ffffff" />
<EditText
android:id="@+id/text_userName"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textColorHint="#cdcdcd"
android:padding="10dp"
android:layout_marginTop="5dp"
android:gravity="center" />
<EditText
android:id="@+id/text_userPassword"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textColorHint="#cdcdcd"
android:padding="10dp"
android:inputType="textPassword"
android:layout_marginTop="5dp"
android:gravity="center" />
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginTop="5dp"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Don't have an account already?" />
</LinearLayout>
</RelativeLayout>
我已经删除了背景和字符串以便在我的电脑中预览。
您需要为第一个布局分配一个 id:
android:id="@+id/first" // insert this code into the first layout
您应该将第二个布局移到第一个布局下方:
android:layout_below="@id/first" // write this code into the second layout