2013-08-21 67 views
0

嗨我有一些问题,在Android中对齐EditText。如何将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)

感谢

+0

更好地使用'GridLayout'。 –

回答

1

添加android:ems="10"XMLEditText性能。数字10是一个任意数字,您可以增加或减少数字,具体取决于您想要EditText多久。

那么,或者您可以将其拖到XMLGraphical Layout选项卡中的相对点。

0

在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> 
+0

android:stretchColumns =“*”如果你只想拉伸所有列的列1你应该使用android:stretchColumns =“1” – 2013-08-21 03:21:28

+0

我编辑你的代码 – 2013-08-21 03:28:26

+0

它正在扩展“显示密码TexView”以及 – dhssa

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> 
-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个对照,除了最后一个,其长谷在它让你的EditText 3个控制每个表行中什么发生根据第二个控制宽度,它的宽度就是你的复选框。因此将CheckBox和TextView放入一个线性布局中将其视为一个控件(或布局),并根据该控件调整您的EditText。