如何在相对布局中居中两个视图?

时间:2013-07-08 15:31:37

标签: android alignment relativelayout center

任务很简单:有两个按钮,上面有一个TextView。所有小部件都应该在相对布局中居中。我唯一的想法是创建第三个小部件View并将其用作按钮的中心轴。有任何想法吗?冗余布局不是一个好的解决方案。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/tv_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/app_name" />

    <View
        android:id="@+id/view_axis"
        android:layout_width="1dp"
        android:layout_height="1dp"
        android:layout_below="@id/tv_progress"
        android:layout_centerInParent="true" />

    <Button
        android:id="@+id/button_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_progress"
        android:layout_toLeftOf="@id/view_axis"
        android:text="@string/start" />

    <Button
        android:id="@+id/button_stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_progress"
        android:layout_toRightOf="@id/view_axis"
        android:text="@string/stop" />

</RelativeLayout>

3 个答案:

答案 0 :(得分:6)

如果我理解你想要的正确,你可以将Button放在LinearLayout中并将其置于中心

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/tv_progress"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="@string/app_name" />
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_below="@id/tv_progress">
<Button
    android:id="@+id/button_start"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/start" />

<Button
    android:id="@+id/button_stop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/stop" />
 </LinearLayout>

我不确定这是否是“冗余布局”的意思,但如果它能满足您的需求,那么这样做很好。

答案 1 :(得分:3)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

    <Spinner
        android:id="@+id/sp_rooms"
        android:layout_toLeftOf="@id/space"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Space
        android:id="@+id/space"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn_registration"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/space"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

答案 2 :(得分:0)

这将垂直和水平居中整个块,包括textview + buttons

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_centerInParent="true">

        <TextView
            android:id="@+id/tv_progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name" />

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

            <Button
                android:id="@+id/button_start"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/start" />

            <Button
                android:id="@+id/button_stop"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/stop" />      

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>
相关问题