ImageView 不能在 RelativeLayout 中居中

ImageView cannot be centered in a RelativeLayout

我已经尝试添加 layout_gravity, and gravity, and centerInParent..但图像视图仍然显示在屏幕的左上角..我是否遗漏了什么?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

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

    <ImageView
        android:id="@+id/noHistroySign"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:gravity="center"
        android:background="@drawable/sign"
        />
    ....

将此代码放入您的图像视图

android:layout_centerVertical="true"
android:layout_centerHorizontal="true"

在 属性 下面使用的 RelativeLayout 中,我们将 ImageView 或其他子项 view.Just 居中,在 ImageView 中添加下面一行属性.

android:layout_centerVertical="true"

移除

    android:layout_gravity="center"
    android:gravity="center"

然后写

    android:src="@drawable/sign"

代替

    android:background="@drawable/sign"

试试这个

<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"
    tools:context=".MainActivity" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/sign" />

</RelativeLayout>

或者试试这个

<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"
    android:gravity="center"
    tools:context=".MainActivity" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/sign" />

</RelativeLayout>

我测试了你的布局。您的 ImageView 已经在中间。没错。

我认为 android:background="@drawable/sign" 中存在问题,可能标志图像未正确显示或未找到或未在可绘制文件夹中。

尝试更换不同的图像,其他图像将完美居中。