【问题标题】:Vertically centering a view in Android在Android中垂直居中视图
【发布时间】:2010-12-13 05:15:02
【问题描述】:

我正在尝试使用以下布局在屏幕上垂直居中 View

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <EditText 
        android:text="example text"  
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical" />
</LinearLayout>

但是它不起作用。 EditText 仍位于屏幕顶部。有人可以解释我在这里做错了什么吗?

注意:如果我将center_horizontal 添加到layout_gravity 属性,则它会水平居中,但仍不会垂直居中。

更新: 在父级上使用 android:gravity="center_vertical" 有效。我还是不明白为什么android:layout_gravity="center_vertical" 对孩子不起作用。

【问题讨论】:

  • 我遇到了这个问题,就像你在更新中所说的那样。我到处寻找,但没有答案。你知道了吗?根据定义,layout_gravity 应该做我们想做的事,但它不起作用

标签: android user-interface layout


【解决方案1】:

我在 Google 群组中跟踪答案,here it is © Romain Guy:

首先,RelativeLayout 忽略了 layout_gravity。然后你需要知道重力意味着“将重力应用到这个视图的内容”,而 layout_gravity 的意思是“应用重力到它的父视图中的这个视图”。因此在 TextView 上,重力会将文本对齐到 TextView 的边界内,而 layout_gravity 会将 TextView 对齐到其父级的边界内。

【讨论】:

  • 这似乎与我上面所说的没有冲突(尤其是在“更新”中),但它不能正常工作。
  • 如何解决上述问题? OP 根本没有使用 RelativeLayout,它没有回答为什么它不起作用。
  • 同意上面的评论,这个不应该选答案
  • 这是一个不错的答案,因为它提供了在父节点上设置重力以实现相同结果的替代方法。它确实帮助了我。
  • 更明显的答案是:将 android:gravity="center_vertical" 添加到父(包含)视图
【解决方案2】:

您可以通过三种方式将视图垂直居中。我建议现在使用 ConstraintLayout。

1。水平线性布局

关键是orientation="horizo​​ntal"。对于orientation="vertical",您不能水平居中。

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

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="example text"/>

</LinearLayout>

2。相对布局

使用RelativeLayout,您可以使用layout_centerVertical="true"

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

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="example text"/>

</RelativeLayout> 

3。约束布局

给你看这个是最简单的。

这里是 XML。它仍然需要添加一个水平约束。

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.constraintlayout.MainActivity"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="81dp">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text="example text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="16dp"/>

</android.support.constraint.ConstraintLayout>

【讨论】:

    【解决方案3】:

    简单快速的答案是将android:gravity="center_vertical" 添加到父(包含)视图。想知道原因的可以参考@Bostone 的回答。

    【讨论】:

      猜你喜欢
      • 2011-10-05
      • 2021-12-25
      • 1970-01-01
      • 2017-12-13
      • 2019-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-24
      相关资源
      最近更新 更多