2013-08-21 65 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。