How to center a View inside of an Android Layout?

后端 未结 10 1430
深忆病人
深忆病人 2020-12-07 18:18

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

相关标签:
10条回答
  • 2020-12-07 18:48

    I was able to center a view using

    android:layout_centerHorizontal="true" 
    

    and

    android:layout_centerVertical="true" 
    

    params.

    0 讨论(0)
  • 2020-12-07 18:53
    <?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>
    
    0 讨论(0)
  • 2020-12-07 18:56

    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:

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

    0 讨论(0)
  • 2020-12-07 18:58

    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 
        android:id="@+id/tv_to_be_centered"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:gravity="center"
        android:text="Some text"
    />
    
    0 讨论(0)
  • 2020-12-07 18:58

    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 ConstraintLayout is more powerful than the RelativeLayout for more complex layouts. So it is worth learning how do do now.

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

    0 讨论(0)
  • 2020-12-07 19:00

    It will work for that code sometimes need both properties

    android:layout_gravity="center"
    android:layout_centerHorizontal="true"
    
    0 讨论(0)
提交回复
热议问题