在 Android 中并排对齐三个控件

Align three control side by side in Android

我是新来的 Android 我想像这样放置控件,但我不能:


textview1

edittext1 edittext2 按钮

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum = "1.0"
    android:orientation="horizontal"
    android:background = "#faadadad" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Final"
        android:id="@+id/textView4"
        android:layout_gravity="center_horizontal" />

     <LinearLayout android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.8">
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft = "2dip"
        android:layout_marginTop = "1dip"
        android:layout_marginBottom = "1dip"
        android:id="@+id/edit1"/>
</LinearLayout>
<LinearLayout android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.8">
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft = "2dip"
        android:layout_marginTop = "1dip"
        android:layout_marginBottom = "1dip"
        android:id="@+id/edit2" />
</LinearLayout>
    <Button android:text = "grade:"
        android:layout_width = "0dip"
        android:layout_height="wrap_content"
        android:layout_weight = "0.2"/>


</LinearLayout>

使用上面的代码,edittext1 和 edittext2 以及 textview2 显示在屏幕外和 textview1 下方

您可以使用此 xml 作为示例,只需替换为您的值

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum = "1.0"
    android:orientation="vertical"
    android:background = "#faadadad" >

    <TextView
        android:id="@+id/textView4"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:text="Final"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/EditText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="1dip"
            android:layout_marginLeft="2dip"
            android:layout_marginTop="1dip"
            android:hint="Search Terms" />

        <EditText
            android:id="@+id/EditText1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="1dip"
            android:layout_marginLeft="2dip"
            android:layout_marginTop="1dip"
            android:hint="Another" />

        <Button
            android:id="@+id/Button"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:text="grade:" />
    </LinearLayout>

</LinearLayout>

输出:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

删除:

    android:weightSum = "1.0"

变化:

    android:orientation="horizontal"

垂直:

    android:orientation="vertical"

没关系

    android:background = "#faadadad" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Final"
        android:id="@+id/textView4"
        android:layout_gravity="center_horizontal" />

变化:

    <LinearLayout android:layout_width="0dp"

至:

    <LinearLayout android:layout_width="match_parent"

还行:

        android:layout_height="wrap_content"

删除:

        android:layout_weight="0.8">

还行:

        <EditText

变化:

            android:layout_width="fill_parent"

至:

            android:layout_width="0dp"

还行:

            android:layout_height="wrap_content"

添加:

            android:layout_weight="1"

还行:

            android:layout_marginLeft = "2dip"
            android:layout_marginTop = "1dip"
            android:layout_marginBottom = "1dip"
            android:hint="Search Terms" />

在这里您可以尝试添加另一个 EditText(复制粘贴)

删除:

    </LinearLayout>

还行:

    <Button android:text = "grade:"
        android:layout_width = "0dip"
        android:layout_height="wrap_content"

变化:

        android:layout_weight = "0.2"/>

至:

        android:layout_weight = "1"/>

添加:

    </LinearLayout>

还行:

</LinearLayout>