[英]Button does not center inside RelativeLayout that have gravity=center
I have a button inside relative layout that I want to center along with input edit text. 我在相对布局中有一个按钮,我希望将其与输入编辑文本对齐。 I added
android:gravity="center"
to relative layout, every was centered correctly other than the button, I even tried to add android:layout_centerInParent="true"
to the button but it didn't work. 我添加了
android:gravity="center"
到相对布局,除了按钮之外每个都正确居中,我甚至试图将android:layout_centerInParent="true"
到按钮但是它不起作用。 What could be the issue? 可能是什么问题?
<LinearLayout 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="bottom">
<ImageView
android:id="@+id/imageView"
android:src="@drawable/logo"
android:layout_weight=".3"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"/>
<RelativeLayout
android:layout_weight=".1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<EditText
android:id="@+id/et_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:inputType="textEmailAddress"
android:ems="10"
android:hint="Email"/>
<EditText
android:id="@+id/et_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_email"
android:inputType="textPassword"
android:hint="Password"
android:ems="10"
android:layout_marginBottom="20dp"
android:imeOptions="actionDone"/>
<Button
android:id="@+id/b_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_password"
android:text="Login"/>
</RelativeLayout>
<View
android:layout_weight=".1"
android:layout_width="match_parent"
android:layout_height="0dp"/>
Button is not centered: 按钮未居中:
android:gravity="center"
is a property of LinearLayout
so its direct child (the RelativeLayout
) inside of it. android:gravity="center"
是LinearLayout
一个属性,所以它的直接子( RelativeLayout
)就在它里面。 You still need to center the children inside the RelativeLayout
. 您仍然需要将子项置于
RelativeLayout
。 To center the Button
inside of its parent RelativeLayout
use android:layout_centerHorizontal="true"
要将
Button
置于其父RelativeLayout
内部,请使用android:layout_centerHorizontal="true"
<Button
android:id="@+id/b_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_password"
android:text="Login"
android:layout_centerHorizontal="true"/> // here
Update 更新
Center each View
inside of the RelativeLayout
将每个
View
放在RelativeLayout
内部
<RelativeLayout
android:layout_weight=".1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<EditText
android:id="@+id/et_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:inputType="textEmailAddress"
android:ems="10"
android:hint="Email"
android:layout_centerHorizontal="true"/>
<EditText
android:id="@+id/et_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_email"
android:inputType="textPassword"
android:hint="Password"
android:ems="10"
android:layout_marginBottom="20dp"
android:imeOptions="actionDone"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/b_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_password"
android:text="Login"
android:layout_centerHorizontal="true"/>
Try this.. Use LinearLayout
instead of RelativeLayout
试试这个..使用
LinearLayout
而不是RelativeLayout
<LinearLayout
android:layout_weight=".1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<EditText
android:id="@+id/et_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:inputType="textEmailAddress"
android:ems="10"
android:hint="Email"/>
<EditText
android:id="@+id/et_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_email"
android:inputType="textPassword"
android:hint="Password"
android:ems="10"
android:layout_marginBottom="20dp"
android:imeOptions="actionDone"/>
<Button
android:id="@+id/b_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_password"
android:text="Login" />
</LinearLayout>
OR in Your same layout just remove android:gravity="center"
add android:layout_centerHorizontal="true"
for each EditText
and Button
或者在你的相同布局中删除
android:gravity="center"
android:layout_centerHorizontal="true"
为每个EditText
和Button
添加android:layout_centerHorizontal="true"
<RelativeLayout
android:layout_weight=".1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/et_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:inputType="textEmailAddress"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Email"/>
<EditText
android:id="@+id/et_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_email"
android:inputType="textPassword"
android:hint="Password"
android:layout_centerHorizontal="true"
android:ems="10"
android:layout_marginBottom="20dp"
android:imeOptions="actionDone"/>
<Button
android:id="@+id/b_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_password"
android:layout_centerHorizontal="true"
android:text="Login" />
</RelativeLayout>
Try this code: 试试这段代码:
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_gravity="bottom">
<ImageView
android:id="@+id/imageView"
android:src="@drawable/logo"
android:layout_weight=".3"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"/>
<RelativeLayout
android:layout_weight=".1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<EditText
android:id="@+id/et_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:inputType="textEmailAddress"
android:ems="10"
android:hint="Email"/>
<EditText
android:id="@+id/et_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_email"
android:inputType="textPassword"
android:hint="Password"
android:ems="10"
android:layout_marginBottom="20dp"
android:imeOptions="actionDone"/>
<Button
android:id="@+id/b_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_password"
android:layout_gravity="center"
android:text="Login"/>
</RelativeLayout>
<View
android:layout_weight=".1"
android:layout_width="match_parent"
android:layout_height="0dp"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.