我想设计一个登录界面。内容应水平和垂直居中。到目前为止,我已经创建了这个:
目前我已经将 marginTop 赋予了我的顶部元素,即图标。但我认为这不是正确的方法。我希望用户名字段位于屏幕的垂直中心。这会将整个内容移动到屏幕中间。
到目前为止,这是我的布局代码:
<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"
tools:context=".LoginActivity"
android:background="#d1d6df">
<ImageView
android:id="@+id/imgIcon"
android:src="@drawable/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Icon"
android:layout_marginBottom="20dp"
android:layout_marginTop="120dp"
android:layout_centerHorizontal="true"/>
<EditText
android:id="@+id/txtUsername"
android:hint="Username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/imgIcon"
android:ems="10"
android:inputType="text">
<requestFocus />
</EditText>
<EditText
android:id="@+id/txtPassword"
android:hint="@string/passwordHint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/txtUsername"
android:ems="10"
android:inputType="textPassword">
</EditText>
<Button
android:text="@string/loginButtonText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/txtPassword"
/>
</RelativeLayout>
此外,我还希望登录按钮从其上级兄弟(即密码字段)获取宽度。这可能吗?
最佳答案
- 删除
marginTop
并在relativeLayout
上添加android:gravity="center"
- 将
android:layout_alignLeft="@+id/txtPassword"
和android:layout_alignRight="@+id/txtPassword"
添加到您的按钮
希望这对你有帮助:)
关于android - 居中对齐的登录屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14362648/