繁体   English   中英

Android相对布局中心垂直边距

[英]Android relative layout center vertical with margin

在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

但我需要marginTop有点底部,我尝试添加marginTop但似乎在使用layout_centerVertical=true它变得不可能。 有解决方案吗

预期结果(略微下调):

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

试试这个:

尝试使用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>

尝试使用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>

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

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

希望这会帮助你。

我今天遇到了这种问题,我想通了这种方式。 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>

如果你不打算为视图制作翻译动画,你可以使用android:translationY属性按你想要的方式推送它(推高的负值)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM