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

13

任务很简单:有两个按钮和一个在它们上面的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>

看这里,这个回答解决了问题,被标记的答案并不是最初所问的。https://dev59.com/Emgv5IYBdhLWcg3wBMSg#13280255 - joe1806772
3个回答

8
如果我正确理解您的意思,您可以将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>

我不确定你所说的“redundant layout”是否是这个意思,但如果这样做可以得到你想要的结果,那么这样做是可以的。


2
当然可以。但这是一个冗余的LinearLayout。在我阅读了Android layout tricks #3Android layout tricks #1之后,我开始更加谨慎地构建布局。 - Maksim Dmitriev
2
以这种方式使用不会对你造成伤害。也许在 ListView 中使用时会有问题,但如果不是的话,这就不是一个问题。你不希望有太多不必要的嵌套 layouts,但在这种情况下,这是我认为最简单的方法。因此它并非多余而且只有一个嵌套的 layout - codeMagic
谢谢!我同意,在这种情况下嵌套的 LinearLayout 是可以的。 - Maksim Dmitriev

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

0

这将垂直和水平居中由TextView和按钮组成的整个块

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

1
你这样添加一个不必要的 LinearLayout - codeMagic
你如何让整个块居中?按照你的方法,TextView 会居中显示,按钮会出现在它们下方。 - Ken Wolf
“Button”将出现在居中的“TextView”下方。我认为这就是OP想要的,但可能有所不同。我还没有比较这两个“layout”,但它们应该具有相同的效果。 - codeMagic
当然,是水平居中。如果他们想要整个块(textview和按钮)水平垂直居中,我已经发布了一个解决方案。使用您的方法,如果您明白我的意思,垂直“加权”将会失衡。在视觉上,屏幕的下半部分会比上半部分更重,因为textview将会命中中心垂直点,而按钮将出现在其下方。 - Ken Wolf
1
朋友,你说得对 :) 我猜这取决于具体需要什么。 - codeMagic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接