如何在 Android 布局中居中视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1499258/
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:09:12  来源:igfitidea点击:

How to center a View inside of an Android Layout?

androidandroid-layoutandroid-view

提问by Vicky

I want to put a layout in the center of the screen.

我想在屏幕中央放置一个布局。

回答by CommonsWare

Step #1: Wrap whatever it is you want centered on the screen in a full-screen RelativeLayout.

第 1 步:将您想要的任何内容以全屏方式包裹在屏幕中央RelativeLayout

Step #2: Give that child view (the one, which you want centered inside the RelativeLayout) the android:layout_centerInParent="true"attribute.

第 2 步:为该子视图(您希望在 内居中的那个RelativeLayout)提供android:layout_centerInParent="true"属性。

回答by Eric Mill

You can apply a centering to any View, including a Layout, by using the XML attribute android:layout_gravity". You probably want to give it the value "center".

您可以通过使用 XML 属性 android:layout_gravity" 将居中应用于任何视图,包括布局。您可能希望为其提供值“center”。

You can find a reference of possible values for this option here: http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html#attr_android:layout_gravity

您可以在此处找到此选项的可能值的参考:http: //developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html#attr_android: layout_gravity

回答by Pentium10

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center">
    <ProgressBar
        android:id="@+id/ProgressBar01"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_gravity="center"
        android:layout_height="wrap_content"></ProgressBar>
    <TextView
        android:layout_below="@id/ProgressBar01"
        android:text="@string/please_wait_authenticating"
        android:id="@+id/txtText"
        android:paddingTop="30px"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></TextView>
</RelativeLayout>

回答by Krishna Singh

I was able to center a view using

我能够使用

android:layout_centerHorizontal="true" 

and

android:layout_centerVertical="true" 

params.

参数。

回答by Suragch

Updated answer: Constraint Layout

更新答案:约束布局

It seems that the trend in Android now is to use a Constraint layout. Although it is simple enough to center a view using a RelativeLayout(as other answers have shown), the ConstraintLayoutis more powerful than the RelativeLayoutfor more complex layouts. So it is worth learning how do do now.

现在看来,Android 的趋势是使用 Constraint 布局。尽管使用 a 使视图居中很简单RelativeLayout(如其他答案所示),ConstraintLayoutRelativeLayout对于更复杂的布局,它比 更强大。所以现在怎么做是值得学习的。

To center a view, just drag the handles to all four sides of the parent.

要将视图居中,只需将手柄拖动到父级的所有四个边。

enter image description here

在此处输入图片说明

回答by Maksim Dmitriev

If you want to center one view, use this one. In this case TextView must be the lowermost view in your XML because it's layout_height is match_parent.

如果要使一个视图居中,请使用此视图。在这种情况下,TextView 必须是 XML 中最下面的视图,因为它的 layout_height 是 match_parent。

<TextView 
    android:id="@+id/tv_to_be_centered"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:gravity="center"
    android:text="Some text"
/>

回答by Md Nazrul Islam

It will work for that code sometimes need both properties

它适用于该代码有时需要两个属性

android:layout_gravity="center"
android:layout_centerHorizontal="true"

回答by Rasika Sugathadasa

Add android:layout_centerInParent="true" to element which you want to center in the RelativeLayout

将 android:layout_centerInParent="true" 添加到要在 RelativeLayout 中居中的元素

回答by live-love

Use ConstraintLayout. Here is an example that will center the view according to the width and height of the parent screen:

使用约束布局。这是一个示例,它将根据父屏幕的宽度和高度将视图居中:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#FF00FF"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHeight_percent=".6"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent=".4"></LinearLayout>
</android.support.constraint.ConstraintLayout>

You might need to change your gradle to get the latest version of ConstraintLayout:

您可能需要更改您的 gradle 以获得最新版本的 ConstraintLayout:

dependencies {
...
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
}

enter image description here

在此处输入图片说明

回答by James Idowu

I use android:layout_centerInParent="true" and it worked

我使用 android:layout_centerInParent="true" 并且它有效