Android 如何将编辑文本与布局对齐?

Android 如何将编辑文本与布局对齐?,android,xml,layout,android-edittext,Android,Xml,Layout,Android Edittext,嗨,我在Android中对齐EditText时遇到了一些问题 我的代码: <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:padding

嗨,我在Android中对齐EditText时遇到了一些问题

我的代码:

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<LinearLayout
    android:id="@+id/header"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/sym_def_app_icon" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title goes here" />

</LinearLayout>

<TableLayout
    android:id="@+id/middleSection"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/header" >

    <TableRow
        android:id="@+id/userRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >


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

        <EditText
        android:id="@+id/userEdit" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    </TableRow>

    <TableRow
        android:id="@+id/passwordRow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

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

        <EditText
        android:id="@+id/passordEdit" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    </TableRow>

    <TableRow
        android:id="@+id/checkBoxRow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

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

        <CheckBox 
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <TextView
            android:id="@+id/checkBoxText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Show Password"/>


    </TableRow>



</TableLayout>

</RelativeLayout>
实际上,我想要:

Username i__________________i
Password i__________________i
         chBox Show Password
我不知道如何调整它,因为我是Android(和XML)新手


谢谢

在TableLayout标签中使用android:stretchColumns=“*”来拉伸您想要的列

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<LinearLayout
    android:id="@+id/header"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/sym_def_app_icon" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title goes here" />

</LinearLayout>

<TableLayout
    android:id="@+id/middleSection"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/header"
    android:stretchColumns="1"
     >

    <TableRow
        android:id="@+id/userRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >


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

        <EditText
        android:id="@+id/userEdit" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    </TableRow>

    <TableRow
        android:id="@+id/passwordRow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

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

        <EditText
        android:id="@+id/passordEdit" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    </TableRow>

    <TableRow
        android:id="@+id/checkBoxRow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/checkBoxBlank"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" "/>
<LinearLayout android:layout_width="fill_parent"
              android:layout_height="wrap_content"

    >
        <CheckBox 
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <TextView
            android:id="@+id/checkBoxText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Show Password"/>
</LinearLayout>

    </TableRow>



</TableLayout>

</RelativeLayout>

试试这个。它会起作用的

<RelativeLayout
    android:id="@+id/header"
    android:layout_width="match_parent"
    android:layout_height="55dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:src="@android:drawable/sym_def_app_icon" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title goes here"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

    <!--ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:src="@android:drawable/sym_def_app_icon" /-->

</RelativeLayout>

<LinearLayout
    android:id="@+id/middleSection"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/header" 
    android:orientation="vertical"
    >

    <LinearLayout
        android:id="@+id/userRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:orientation="horizontal"
        >


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

        <EditText
        android:id="@+id/userEdit" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/passwordRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

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

        <EditText
        android:id="@+id/passordEdit" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/checkBoxRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <CheckBox 
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"/>

        <TextView
            android:id="@+id/checkBoxText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Show Password"/>


    </LinearLayout>



</LinearLayout>

</LinearLayout>


android:ems=“10”
添加到
XML
中的
EditText
属性中。数字10是一个任意数字,您可以根据需要编辑文本的时间长短来增加或减少它

或者您可以将其拖到
XML
Graphical Layout
选项卡中的相对点


<CheckBox 
                android:id="@+id/checkBox"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

    <TextView
                android:id="@+id/checkBoxText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Show Password"/>
将上述代码放在一个父布局中,如线性布局所示:

Username i__i
Password i__I
         chBox Show Password
<LinearLayout
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
            >
        <CheckBox 
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <TextView
            android:id="@+id/checkBoxText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Show Password"/>

</LinearLayout>


发生的情况是,除了最后一个控件外,每个表行中都有2个控件,而最后一个控件中有3个控件,因此EditText的宽度取决于第二个控件的宽度,这是您的复选框。因此,将复选框和TeXVIEW放在一个线性布局中,将其视为一个控件(或布局),并根据此调整编辑文本。

Android:StudioStudio=“*”拉伸表的所有列,如果只需要弹出列1,则应该使用Android:StudioStudio =“1”更好地使用<代码> GridLayout < /代码>。