屏幕元素中心 android xml

Elements Center of screen android xml

我想在任何设备的屏幕中居中放置两个元素我的元素是一个 ImageView 和一个具有 3 个文本视图的相对布局

我有这个代码

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

而且我想要图像视图和屏幕中间的相对布局,一个挨着一个。

我用 this 和其他解决方案进行了探索,但它对我不起作用

将您的 ImageView 和具有 3 个文本视图的相关布局放入 LinearLayout(根据您的要求垂直或水平)。

并将这个 LinearLayour 放在 RelativeLayout 中。并添加

android:layout_centerInParent="true"

线性布局。 它将如下所示

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