【问题标题】:Android relative layout center vertical with marginAndroid相对布局中心垂直与边距
【发布时间】:2015-01-14 04:49:14
【问题描述】:

在 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

【问题讨论】:

  • 好问题,谢谢

标签: android layout center margin vertical-alignment


【解决方案1】:

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

【讨论】:

  • 仅使用相对布局即可完成,无需复杂的视图层次结构。请看下面我的回答
  • @amarkovits,我认为您没有看到所有答案,请检查所有答案,因为您已经将某个答案作为您的类型解决方案,因此请尝试对此进行投票,而不是放置您的知名答案。
【解决方案2】:

试试这个:

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

【讨论】:

    【解决方案3】:

    我今天遇到了这种问题,我想出了这个办法。 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>
    

    【讨论】:

      【解决方案4】:

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

      使用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,这不起作用
      【解决方案5】:

      如果您不打算为视图制作平移动画,您可以使用android:translationY 属性将其推送任意多(向上推送为负值)

      【讨论】:

      • 我不知道有人否决这个答案的原因,但这正是不使用复杂层次结构或空间属性的方法。
      猜你喜欢
      • 2013-05-04
      • 2013-11-23
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      • 2018-06-20
      • 2019-04-10
      • 2017-09-27
      相关资源
      最近更新 更多