安卓相对布局如何在垂直方向上居中并设置边距?

7
在Android的RelativeLayout中,我们可以使用以下代码将textView精确地放置在屏幕中央:
<TextView
    android:text="This is TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

结果:

top
-
-
-
-
This is TextView (center vertical)
-
-
-
-
bottom

我希望textView能够往下移动一点,我尝试添加marginTop,但似乎在使用layout_centerVertical=true后变得不可能。有解决方法吗?
期望结果(稍微往下):
top
-
-
-
-
-
-
This is TextView (center vertical slight to bottom)
-
-
bottom

很棒的问题,谢谢。 - smoothumut
5个回答

11

试试这个:

尝试使用RelativeLayout,它可以通过权重轻松实现您的要求:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical">
      <TextView
          android:id="@id/dummy"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerVertical="true"
          android:layout_centerHorizontal="true" />
      <TextView
          android:layout_below="@id/dummy"
          android:layout_marginTop="10dp"
          android:text="This is TextView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"/>
</RelativeLayout>

7

为什么要在父视图中添加嵌套视图?

可以使用paddingTop属性。

示例:

<RelativeLayout
    android:id="@+id/rlParentView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:paddingTop="10dp"/>

</RelativeLayout>

希望这可以帮助您。

如果宽度和高度有固定值,如50dp,则此方法无效。 - ACAkgul

6

尝试使用LinearLayout,可以使用权重轻松满足您的要求:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center_vertical">
        <TextView
            android:text="This is TextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

可以仅使用RelativeLayout来完成,无需拥有复杂的视图层次结构。请参见下面的答案。 - amarkovits
@amarkovits,我觉得你可能没有看到所有的答案,请检查一下所有的答案,因为有人已经提供了你所写的解决方案,请尝试投票支持它,而不是发布重复的答案。 - Haresh Chhelana

4

如果您不打算为视图制作翻译动画,您可以使用 android:translationY 属性将其向上推动任意距离(负值表示向上推动)。


2
我不知道有人为什么会对这个答案进行负投票,但这正是可以在不使用复杂层次结构或空间属性的情况下工作的方法。 - B L Λ C K

1

今天我遇到了这样的问题,我找到了解决方法。SapceLinearLayout是实现此目的的好方法。

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/base_background_color">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/base_toolbar_color"
        android:minHeight="?attr/actionBarSize"/>

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar"/>

    <!-- using LinearLayout to align ProgressBar to center vertical -->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:orientation="vertical">

        <Space
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"/>

        <ProgressBar
            android:id="@+id/other_profile_progressBar"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:indeterminate="false"/>
    </LinearLayout>
</RelativeLayout>

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