在Android中并排对齐三个控件

时间:2015-04-19 19:16:26

标签: android xml android-layout android-widget alignment

我是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下

2 个答案:

答案 0 :(得分:0)

您可以使用此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>

输出:

enter image description here

答案 1 :(得分:0)

<?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>