0

嗨,我在 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

实际上,我想要:

Username i__________________i
Password i__________________i
         chBox Show Password

我不知道如何对齐它,因为我是 Android(和 XML)的新手

谢谢

4

4 回答 4

1

添加android:ems="10"到. EditText_ XML数字 10 是一个任意数字,您可以根据需要多长时间来增加或减少它EditText

那,或者您可以将其拖更长的时间Graphical LayoutXML.

于 2013-08-21T05:02:21.980 回答
0

android:stretchColumns="*"在 TableLayout 标记中使用拉伸您想要的列

<?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>
于 2013-08-21T03:19:47.847 回答
0

尝试这个。它会起作用的。

<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>
于 2013-08-21T04:33:41.467 回答
-1
<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:

<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 的宽度取决于您的复选框的第二个控件宽度。因此将 CheckBox 和 TextView 放在一个线性布局中,将其视为一个控件(或布局),并据此调整您的 EditText。

于 2013-08-21T07:15:11.723 回答