如何创建包含带有按钮的文本视图的布局

时间:2012-03-08 13:35:20

标签: android android-layout

如何使用textview两侧的按钮创建布局?这是我到目前为止所得到的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/left_button"
        android:layout_width="72dip"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="left" />

    <TextView
        android:id="@+id/center_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dip"
        android:padding="10dip"
        android:text="textview" />

    <Button
        android:id="@+id/right_button"
        android:layout_width="72dip"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="right" />
</RelativeLayout>

这里的问题是textview是居中的。我希望textview填充剩下的完整空间。我尝试将其android:layout_width设置为fill_parent并删除android:layout_centerHorizo​​ntal,但它似乎与按钮重叠。

5 个答案:

答案 0 :(得分:5)

<强>步骤1

您需要在屏幕左侧创建提示android:layout_alignParentLeft="true"

的第一个按钮

<强>步骤-2

在屏幕右侧创建提示android:layout_alignParentRight="true"

的第二个按钮

<强>步骤-3

现在创建TextView并在第一个按钮右侧提及它,并在第二个按钮左侧提及

android:layout_toRightOf="@id/left_button"
    android:layout_toLeftOf="@+id/right_button"

XML代码

<?xml version="1.0" encoding="utf-8"?>

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

        <Button
            android:id="@+id/left_button"
            android:layout_width="72dip"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="left" />

        <Button
            android:id="@+id/right_button"
            android:layout_width="72dip"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="right" />
        <TextView
            android:id="@+id/center_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dip"
            android:layout_toRightOf="@id/left_button"
            android:layout_toLeftOf="@+id/right_button"
            android:padding="10dip"
            android:text="textview" />


    </RelativeLayout>

答案 1 :(得分:2)

听起来你会更好地服务于LinearLayout,如下所示:

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

    <Button
        android:id="@+id/left_button"
        android:layout_width="72dip"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:text="left" />

    <TextView
        android:id="@+id/center_textview"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:layout_weight="1"
        android:padding="10dip"
        android:text="textview" />

    <Button
        android:id="@+id/right_button"
        android:layout_width="72dip"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:text="right" />
</RelativeLayout>

这样三个视图都将在一个水平线上,TextView将在按钮布局后占用任何剩余空间(参见android:layout_weight属性)。

答案 2 :(得分:0)

这些属性可以解决您的问题

android:layout_toLeftOf
android:layout_toRightOf

答案 3 :(得分:0)

尝试使用orientationL为“水平”的LinearLayout,与RelativeLayout相对。

或者如果你想做RelativeLayout,你可以使用Chandra提到的属性(即,将textview相对于按钮对齐,而不是相对于父容器。)

答案 4 :(得分:0)

不确定你想要什么,但你可以使用例如。 layout_toRightOf。将按钮与父对齐对齐然后将视图放在左侧的右侧的示例。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<Button
    android:id="@+id/left_button"
    android:layout_width="72dip"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:text="left" />

<TextView
    android:id="@+id/center_textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dip"
    android:layout_toRightOf="@id/left_button"
    android:padding="10dip"
    android:text="textview" />

<Button
    android:id="@+id/right_button"
    android:layout_width="72dip"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/center_textview"
    android:text="right" />

</RelativeLayout>