Android 如何将登录表单放置在布局的中心

Android 如何将登录表单放置在布局的中心,android,android-layout,layout,Android,Android Layout,Layout,我正在尝试创建以下布局: |-----------------------------------------------------------| | | | | |

我正在尝试创建以下布局:

|-----------------------------------------------------------|
|                                                           |
|                                                           |
|                                                           |
|                                                           |
|                 L-Email     [___________]-R               |
|                 L-Password  [___________]-R               |
|                                   (Login)-R               |
|                                                           |
|                                                           |
|                                                           |
|-----------------------------------------------------------|
所以我尝试的是创建一个线性布局,每个电子邮件、密码和登录行都是一个线性布局。但我想要的是将整个东西放在中间(垂直和水平),并将电子邮件和密码标签与屏幕的“L-”部分对齐(“L-”只是表示我要在那里对齐),同时将两个文本框和登录按钮与“-R”标志对齐(所以我实际上不需要L和R符号,它们只是表示模型中的对齐位置)

下面是一个更具体的模型:

所以我想将整个东西对齐到中心,并将文本标签对齐到左绿线,而其他标签对齐到右绿线

目前,我更喜欢在eclipse中使用图形编辑器,但欢迎提出任何建议

到目前为止,我已经尝试过这一个,但已被卡住:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/sahbg"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10" >

            <requestFocus />
        </EditText>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <EditText
            android:id="@+id/EditText01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

    </LinearLayout>

</LinearLayout>

看起来是这样的:


使用现有代码:

  • 更改外线布局的高度和宽度:

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    
    (您可能需要将
    gravity=“center”
    更改为
    layout\u gravity=“center”
    ,老实说,我现在不记得哪个是哪个了。)

  • 将“密码”行布局的宽度切换为
    match\u parent
    ,以便“用户名”和“密码”行的宽度相同

  • 移除固定按钮的线性布局,因为它不是必需的,并添加:

    android:layout_gravity="right"
    
    你也可以在编辑文本上使用它,并给它们一个特定的宽度,使它们的大小相同


  • 尝试使用RelativeLayout。如果出于某种原因,您希望使用LinearLayout,请仅使用android:gravity和android:layout\u gravity属性。经过您的修改,现在看起来更好了:现在我需要注意文本视图,我需要在edittext和文本视图之间插入一些空间。但是,我还需要将文本视图与t对齐如我在原始问题中发布的图片所示,他离开了(将文本视图与左绿线对齐)在编辑文本中使用
    layout\u marginLeft=“5dp”
    在文本视图和编辑文本之间添加一个小空间。默认情况下,文本视图应与左侧对齐(如果它们不是添加
    android:layout\u gravity=“left”
    )差不多明白了:代码是:为什么顶部的线性布局偏左了一点?顺便说一下,如果文本视图有相同的文本,布局是可以的,但我当然需要不同的文本。好的,将“用户名”和“密码”线性布局宽度设置为
    android:layout\u width=“match\u parent”
    。这样两个布局的大小都相同。谢谢。我只需将用户名的linearlayout设置为与父项匹配,并将顶部的edittext设置为android:layout\u marginLeft=“62dp”