问题对齐按钮

时间:2011-05-05 01:18:57

标签: android user-interface alignment android-layout-weight

我想将这些输入对齐,右侧有一个自动完成框,右侧是一个较小的按钮。然而,我最终得到了一个覆盖所有内容的大型自动完成(第二个)。

    <RelativeLayout
        android:layout_alignParentBottom="true" 
        android:id="@+id/transport_search"
        android:background="@color/dark_grey"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent">

        <AutoCompleteTextView
            android:id="@+id/transport_autoCompleteFrom"
            android:layout_width="fill_parent"
            android:layout_weight="5"
            ></AutoCompleteTextView>

        <Button
            android:id="@+id/transport_switchDirection"
            android:layout_toRightOf="@+id/transport_autoCompleteFrom"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            ></Button>

        <AutoCompleteTextView
            android:id="@+id/transport_autoCompleteTo"
            android:layout_width="fill_parent"
            android:layout_weight="5"
            ></AutoCompleteTextView>

        <Button
            android:id="@+id/transport_go"
            android:layout_toRightOf="@+id/transport_autoCompleteTo"
            android:layout_width="fill_parent"
            android:layout_weight="1"
            ></Button>

    </RelativeLayout>

我做错了什么?

2 个答案:

答案 0 :(得分:0)

您可以在RelativeLayout中使用LinearLayout。下面显示了两个按钮:

    <LinearLayout android:id="@+id/buttons" android:layout_below="@id/aboveControl1"
    android:layout_width="fill_parent"  android:layout_height="wrap_content" 
    android:orientation="horizontal" android:weightSum="2" 
    android:layout_alignLeft="@id/otherControl1" android:layout_alignRight="@id/otherControl2" >
    <Button android:id="@+id/button10" android:layout_width="fill_parent" 
        android:layout_height="wrap_content" android:text="Ok" android:layout_weight="1" />
    <Button android:id="@+id/button11" android:layout_width="fill_parent" 
        android:layout_height="wrap_content" android:text="Cancel" android:layout_weight="1" />
</LinearLayout>

答案 1 :(得分:0)

如果您更喜欢使用RelativeLayout,可能以下内容可让您更接近您想要实现的目标。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_alignParentBottom="true"
    android:id="@+id/transport_search"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent">
    <AutoCompleteTextView
        android:id="@+id/transport_autoCompleteFrom"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:text="auto 1" />
    <Button
        android:id="@+id/transport_switchDirection"
        android:layout_toRightOf="@id/transport_autoCompleteFrom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button1" />
    <AutoCompleteTextView
        android:id="@+id/transport_autoCompleteTo"
        android:layout_below="@id/transport_autoCompleteFrom"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:text="auto 2" />
    <Button
        android:id="@+id/transport_go"
        android:layout_toRightOf="@id/transport_autoCompleteTo"
        android:layout_below="@id/transport_switchDirection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button2" />
</RelativeLayout>

以下是此布局的屏幕截图。

另外,您可能想看看TableLayout如何用于Android Create aligned fields Layout

中的类似结果
相关问题