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

如何在Android布局中居中显示视图?,android,android-layout,android-view,Android,Android Layout,Android View,我想在屏幕中央放一个布局 您可以使用XML属性android:Layout\u gravity对任何视图(包括布局)应用居中。您可能希望为其指定值“center” 您可以在此处找到此选项可能值的参考: 步骤#1:以全屏RelativeLayout为中心,将您想要的内容包装在屏幕上 步骤2:为该子视图(您希望在相对视图中居中的视图)提供android:layout\u centerInParent=“true”属性。 <?xml version="1.0" encoding="utf-8"?

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

您可以使用XML属性android:Layout\u gravity对任何视图(包括布局)应用居中。您可能希望为其指定值“center”

您可以在此处找到此选项可能值的参考:

步骤#1:以全屏
RelativeLayout
为中心,将您想要的内容包装在屏幕上

步骤2:为该子视图(您希望在
相对视图中居中的视图)提供
android:layout\u centerInParent=“true”
属性。


<?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>

如果要将一个视图居中,请使用此视图。在这种情况下,TextView必须是XML中最低的视图,因为它的布局高度是匹配的

<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"
/>

我可以使用

android:layout_centerHorizontal="true" 


参数。

将android:layout\u centerInParent=“true”添加到要在RelativeLayout中居中的元素中,这样代码有时需要两个属性

android:layout_gravity="center"
android:layout_centerHorizontal="true"
最新答复: Android现在的趋势似乎是使用约束布局。虽然使用
RelativeLayout
(如其他答案所示)将视图居中足够简单,但对于更复杂的布局,
RelativeLayout
RelativeLayout
更强大。因此现在值得学习

若要使视图居中,只需将控制柄拖动到父视图的所有四边


使用ConstraintLayout。以下示例将根据父屏幕的宽度和高度使视图居中:

<?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>
我使用 android:layout\u centerInParent=“true”
好的,layout_gravity只适用于LinearLayout,不适用于任意布局。正如你提到的layout_gravity只适用于LinearLayout,那么RelativeLayout呢?因为在应用程序中,我想在ImageView上嵌入textview,所以我也尝试了RelativeLayout,我认为它可以工作。它不仅适用于LinearLayout,请看这个例子le:
FrameLayout
LinearLayout
都支持子女的
android:layout\u gravity
RelativeLayout
不支持。仅此而已。这仅在我将属性安卓:layout\u centerInParent=“true”放入时对我有效“进入RelativeLayout内的视图。如果我想将其放置在中间,但如果有与另一个视图重叠的危险,则将其向下移动,该怎么办?您需要将
android:layout\u centerInParent=“true”
放在子视图上,而不是
RelativeLayout
<代码>android:layout.*
属性影响视图在父视图中的定位和大小,而不是视图的子视图在父视图中的定位和大小。对于步骤3,我将给出一个大+1。。经典:-D,即使出了问题(但别担心,肯定会有一个v.愚蠢的拼写错误)。他的意思是把布局放在中间,而不是放视图。@IgorG。布局和视图的定位是相同的,因为布局是视图。
layout
widges确实扩展了
ViewGroup
…但是
ViewGroup
扩展了视图。因此
布局
是一个
视图组
视图组
是一个
视图
这种方法只有在所讨论的视图没有背景或点击侦听器等的情况下才有效-基本上,如果你不能判断它正在变得巨大。此外,该视图还需要支持
android:layout\u gravity
属性。请注意,只有当所讨论的视图(直接)位于
RelativeLayout
中时,这些属性才起作用。这正是我需要的!感谢您使用什么工具将此视频录制并创建为gif图片?谢谢大家!@怎么了,我不记得我用了哪一个来制作那个特别的动画gif。这将取决于您的操作系统。我用的是Linux。在谷歌中搜索类似“Linux动画gif生成器”的东西。在Mac上,我现在使用LICEcap。
<?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>
dependencies {
...
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
}