如何在android中对齐字段?

如何在android中对齐字段?,android,Android,你好 我正在用android做一个简单的例子,但我的问题是 我的文本视图和编辑字段不对齐。这意味着 应该是这样的 ![Name editView Rollnumber editView Button on center][2] 看起来像这样 这是我的密码 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns

你好 我正在用android做一个简单的例子,但我的问题是 我的文本视图和编辑字段不对齐。这意味着 应该是这样的

![Name               editView
Rollnumber         editView 


           Button on center][2]
看起来像这样

这是我的密码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
   android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
   >
   <LinearLayout
      android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"  

        >

 <TextView 
        android:id="@+id/text_view_boat1"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Name"
       /> 
    <EditText 
        android:id="@+id/entry" 
        android:hint="add name" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
    /> 

    </LinearLayout>
    <LinearLayout
             android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"  
        >
    <TextView 
        android:id="@+id/text_view_boat2"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Roll Number"
/> 
    <EditText 
        android:id="@+id/entry2" 
        android:hint="add roll number" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
   /> 
        </LinearLayout>



    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me"
        android:id="@+id/clickme" 



        />
</LinearLayout>

改用
表格布局

    <LinearLayout 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:orientation="vertical" >

            <TableLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingTop="10dp"
                android:shrinkColumns="1"
                android:stretchColumns="1" >

                <TableRow
                    android:descendantFocusability="beforeDescendants"
                    android:focusable="true"
                    android:focusableInTouchMode="true" >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Name  :" />

                    <EditText
                        android:id="@+id/companyIdEditText_id"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="" 
                        android:enabled="false"/>
                </TableRow>

                <TableRow
                    android:descendantFocusability="beforeDescendants"
                    android:focusable="true"
                    android:focusableInTouchMode="true" >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="RollNumber  :" />

                    <EditText
                        android:id="@+id/companyNameEditText_id"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="" />
                </TableRow>
    </TableLayout>
 <Button 
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="ClickMe"/>
    </LinearLayout>

改用
表格布局

    <LinearLayout 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:orientation="vertical" >

            <TableLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingTop="10dp"
                android:shrinkColumns="1"
                android:stretchColumns="1" >

                <TableRow
                    android:descendantFocusability="beforeDescendants"
                    android:focusable="true"
                    android:focusableInTouchMode="true" >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Name  :" />

                    <EditText
                        android:id="@+id/companyIdEditText_id"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="" 
                        android:enabled="false"/>
                </TableRow>

                <TableRow
                    android:descendantFocusability="beforeDescendants"
                    android:focusable="true"
                    android:focusableInTouchMode="true" >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="RollNumber  :" />

                    <EditText
                        android:id="@+id/companyNameEditText_id"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="" />
                </TableRow>
    </TableLayout>
 <Button 
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="ClickMe"/>
    </LinearLayout>

改用
表格布局

    <LinearLayout 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:orientation="vertical" >

            <TableLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingTop="10dp"
                android:shrinkColumns="1"
                android:stretchColumns="1" >

                <TableRow
                    android:descendantFocusability="beforeDescendants"
                    android:focusable="true"
                    android:focusableInTouchMode="true" >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Name  :" />

                    <EditText
                        android:id="@+id/companyIdEditText_id"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="" 
                        android:enabled="false"/>
                </TableRow>

                <TableRow
                    android:descendantFocusability="beforeDescendants"
                    android:focusable="true"
                    android:focusableInTouchMode="true" >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="RollNumber  :" />

                    <EditText
                        android:id="@+id/companyNameEditText_id"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="" />
                </TableRow>
    </TableLayout>
 <Button 
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="ClickMe"/>
    </LinearLayout>

改用
表格布局

    <LinearLayout 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:orientation="vertical" >

            <TableLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingTop="10dp"
                android:shrinkColumns="1"
                android:stretchColumns="1" >

                <TableRow
                    android:descendantFocusability="beforeDescendants"
                    android:focusable="true"
                    android:focusableInTouchMode="true" >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Name  :" />

                    <EditText
                        android:id="@+id/companyIdEditText_id"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="" 
                        android:enabled="false"/>
                </TableRow>

                <TableRow
                    android:descendantFocusability="beforeDescendants"
                    android:focusable="true"
                    android:focusableInTouchMode="true" >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="RollNumber  :" />

                    <EditText
                        android:id="@+id/companyNameEditText_id"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="" />
                </TableRow>
    </TableLayout>
 <Button 
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="ClickMe"/>
    </LinearLayout>

您可以使用属性权重。将行的chridren的宽度设置为0dp

<LinearLayout
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"  
>
<TextView 
    android:id="@+id/text_view_boat2"
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"
    android:text="Roll Number"
    /> 
<EditText 
    android:id="@+id/entry2" 
    android:hint="add roll number" 
    android:layout_width="wrap_content" 
    android:layout_height="0dp"
    android:layout_weight="2" 
     /> 
</LinearLayout>

您可以使用属性权重。将行的chridren的宽度设置为0dp

<LinearLayout
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"  
>
<TextView 
    android:id="@+id/text_view_boat2"
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"
    android:text="Roll Number"
    /> 
<EditText 
    android:id="@+id/entry2" 
    android:hint="add roll number" 
    android:layout_width="wrap_content" 
    android:layout_height="0dp"
    android:layout_weight="2" 
     /> 
</LinearLayout>

您可以使用属性权重。将行的chridren的宽度设置为0dp

<LinearLayout
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"  
>
<TextView 
    android:id="@+id/text_view_boat2"
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"
    android:text="Roll Number"
    /> 
<EditText 
    android:id="@+id/entry2" 
    android:hint="add roll number" 
    android:layout_width="wrap_content" 
    android:layout_height="0dp"
    android:layout_weight="2" 
     /> 
</LinearLayout>

您可以使用属性权重。将行的chridren的宽度设置为0dp

<LinearLayout
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"  
>
<TextView 
    android:id="@+id/text_view_boat2"
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"
    android:text="Roll Number"
    /> 
<EditText 
    android:id="@+id/entry2" 
    android:hint="add roll number" 
    android:layout_width="wrap_content" 
    android:layout_height="0dp"
    android:layout_weight="2" 
     /> 
</LinearLayout>

试试这个

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/text_view_boat1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="05dp"
    android:text="Name" />

<EditText
    android:id="@+id/entry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@id/text_view_boat1"
    android:layout_marginRight="05dp"
    android:layout_toRightOf="@id/text_view_boat1"
    android:hint="add name" />

<LinearLayout
    android:id="@+id/lin_lay"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/entry"
    android:layout_marginTop="05dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/text_view_boat2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Roll Number" />

    <EditText
        android:id="@+id/entry2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="add roll number" />
</LinearLayout>

<Button
    android:id="@+id/clickme"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/lin_lay"
    android:layout_marginTop="05dp"
    android:text="Click Me" />

试试这个

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/text_view_boat1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="05dp"
    android:text="Name" />

<EditText
    android:id="@+id/entry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@id/text_view_boat1"
    android:layout_marginRight="05dp"
    android:layout_toRightOf="@id/text_view_boat1"
    android:hint="add name" />

<LinearLayout
    android:id="@+id/lin_lay"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/entry"
    android:layout_marginTop="05dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/text_view_boat2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Roll Number" />

    <EditText
        android:id="@+id/entry2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="add roll number" />
</LinearLayout>

<Button
    android:id="@+id/clickme"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/lin_lay"
    android:layout_marginTop="05dp"
    android:text="Click Me" />

试试这个

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/text_view_boat1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="05dp"
    android:text="Name" />

<EditText
    android:id="@+id/entry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@id/text_view_boat1"
    android:layout_marginRight="05dp"
    android:layout_toRightOf="@id/text_view_boat1"
    android:hint="add name" />

<LinearLayout
    android:id="@+id/lin_lay"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/entry"
    android:layout_marginTop="05dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/text_view_boat2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Roll Number" />

    <EditText
        android:id="@+id/entry2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="add roll number" />
</LinearLayout>

<Button
    android:id="@+id/clickme"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/lin_lay"
    android:layout_marginTop="05dp"
    android:text="Click Me" />

试试这个

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/text_view_boat1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="05dp"
    android:text="Name" />

<EditText
    android:id="@+id/entry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@id/text_view_boat1"
    android:layout_marginRight="05dp"
    android:layout_toRightOf="@id/text_view_boat1"
    android:hint="add name" />

<LinearLayout
    android:id="@+id/lin_lay"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/entry"
    android:layout_marginTop="05dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/text_view_boat2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Roll Number" />

    <EditText
        android:id="@+id/entry2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="add roll number" />
</LinearLayout>

<Button
    android:id="@+id/clickme"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/lin_lay"
    android:layout_marginTop="05dp"
    android:text="Click Me" />