简体   繁体   中英

Elements Center of screen android xml

I want to center two elements in the screen of the any devices my elements are an ImageView and a relative layout with 3 text view

I have this code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_app"
tools:context=".HomeActivity"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_xlogo_splash"
    android:layout_gravity="center"
    android:id="@+id/main_logo"
    />

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_gravity="center">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/suite"
    android:textColor="@color/white"
    android:id="@+id/suite"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/business"
    android:layout_alignEnd="@+id/business" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/business"
    android:textColor="@color/white"
    android:layout_gravity="center"
    android:textSize="30sp"
    android:id="@+id/business"
    android:layout_below="@+id/suite"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/by"
    android:textColor="@color/white"
    android:layout_gravity="center"
    android:id="@+id/by"
    android:layout_below="@+id/business"
    android:layout_centerHorizontal="true" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_xetux_logo"
    android:layout_marginLeft="20dp"
    android:layout_gravity="center"
    android:layout_below="@+id/business"
    android:id="@+id/company_logo"
    android:layout_alignRight="@+id/business"
    />
</RelativeLayout>

And I want the image view and the relative layout in the middle of the screen, one next to each other.

I probed with this and others solutions and it doesn't work to me

Put your ImageView and a relative layout with 3 text view inside LinearLayout(Either vertical or Horizontal as your requirement).

And place this LinearLayour inside RelativeLayout. and Add

android:layout_centerInParent="true"

in Linearlayout. It will look like below

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

<LinearLayout 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_centerInParent="true">

<ImageView..../>

<RelativeLayout...>
    <TextView.../>
    <TextView.../>
    <TextView.../>
</RelativeLayout>

</LinearLayout>
</RelativeLayout>

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