I want to put a layout in the center of the screen.
问题:
回答1:
Step #1: Wrap whatever it is you want centered on the screen in a full-screen RelativeLayout
.
Step #2: Give that child view (the one, which you want centered inside the RelativeLayout
) the android:layout_centerInParent="true"
attribute.
回答2:
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".
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
回答3:
<?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>
回答4:
I was able to center a view using
android:layout_centerHorizontal="true"
and
android:layout_centerVertical="true"
params.
回答5:
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"
/>
回答6:
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.
回答7:
It will work for that code sometimes need both properties
android:layout_gravity="center"
android:layout_centerHorizontal="true"
回答8:
Add android:layout_centerInParent="true" to element which you want to center in the RelativeLayout
回答9:
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'
}