在 Android 中垂直居中视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1677038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:26:53  来源:igfitidea点击:

Vertically centering a view in Android

androiduser-interfacelayout

提问by Jeremy Logan

I'm trying to center a Viewvertically on screen with the following layout:

我正在尝试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>

However it doesn't work. The EditTextis still at the top of the screen. Can someone explain what I'm doing wrong here?

但是它不起作用。在EditText屏幕上方依然。有人可以解释我在这里做错了什么吗?

NOTE:if I add center_horizontalto the layout_gravityattribute then it centers it horizontally, but still does not center vertically.

注意:如果我添加center_horizontallayout_gravity属性,那么它会水平居中,但仍然不会垂直居中。

UPDATE:using android:gravity="center_vertical"on the parent worked. I still don't understand why android:layout_gravity="center_vertical"on the child didn't work.

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

采纳答案by Bostone

I track the answer on Google groups, here it is© Romain Guy:

我在 Google 群组上跟踪答案,这里是© Romain Guy:

Well, first of all RelativeLayout ignores layout_gravity. Then you need to know that gravity means "apply gravity to the content of this view" whereas layout_gravity means "apply gravity to this view within its parent." So on a TextView, gravity will align the text within the bounds of the TextView whereas layout_gravity will align the TextView within the bounds of its parent.

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

回答by didxga

The simple and quick answer is to add android:gravity="center_vertical"to the parent(containing) view. For those who want to know why, please refer to @Bostone 's answer.

简单而快速的答案是添加android:gravity="center_vertical"到父(包含)视图。对于那些想知道原因的人,请参阅@Bostone 的回答。

回答by Suragch

There are three ways you can center a view vertically. I recommend using the ConstraintLayout now.

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

1. Horizontal LinearLayout

1. 水平线性布局

The key is orientation="horizontal". You can't center horizontally for orientation="vertical".

关键是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

2. 相对布局

With a RelativeLayout you can use layout_centerVertical="true".

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

3. 约束布局

It is easiest to show you this one.

向您展示这个是最容易的。

enter image description here

在此处输入图片说明

Here is the XML. It still would need a horizontal constraint to be added.

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