[英]how to align the EditText to the layout?
嗨,我在對齊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)的新手
謝謝
將android:ems="10"
到XML
的EditText
屬性中。 數字10是一個任意數字,您可以根據需要EditText
時間來增加或減少它。
那樣,或者您可以將其拖到XML
的“ Graphical Layout
選項卡中的相對位置更長。
在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>
<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
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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.