简体   繁体   中英

Android-How to center my custom View in LinearLayout?

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal">

    <com.example.root.howold.MyRing
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

I also try to set gravity and layout_gravity , but they don't work actually.

How can I make MyRing(my custom View) center in Layout?

Try this:-

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

    <com.example.root.howold.MyRing
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

Read this: Gravity and layout_gravity on Android

To make your view aligned center, you can:

LinearLayout case1:
"android:gravity" is for it's children views. If this doesn't works, check it's parent view's width and height. And, set parent's width and height to bigger size.

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TEST" />
</LinearLayout>


LinearLayout case2:
"android:layout_gravity" is for it self, if parent view has empty spaces.

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="TEST" />
</LinearLayout>


RelativeLayout
You can use "android:layout_centerInParent" for RelativeLayout.

   <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="TEST" />
</RelativeLayout>

If your using root layout as linear layout. You can use below text that will set your custom view to center.

Depends on your requirement you can user

 layout_gravity="center"

(or)

layout_gravity="center_vertical" 

(or)

layout_gravity="center_horizontal"

Below is the xml which has custom view.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

 <!-- Here you may some other views -->

   <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center">

    <com.purpletalk.root.sampledemo.MyRing
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="hellooooo" />
   </LinearLayout>
</LinearLayout>

If you are using Linear Layout,

Option 1: Add android:gravity to the parent view, to center all its child
views. Example:

<LinearLayout
           android:gravity="center">
           <TextView/>   
           <TextView/>  // Both these textviews will be aligned center
           </LinearLayout>

Option 2: If you want to center only a child view, then add the attribute android:layout_gravity="center" to the childview tag.

<LinearLayout>
           <TextView
            android:layout_gravity="center"  // Only this textview will be aligned center
           />
           <TextView/>
           </LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM