【问题标题】:ImageView cannot be centered in a RelativeLayoutImageView 不能在 RelativeLayout 中居中
【发布时间】:2015-10-20 11:21:04
【问题描述】:

我已经尝试添加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"
        />
    ....

【问题讨论】:

  • 只需将RelativeLayout的重力设置为中心即可。那应该可以。
  • 您能向我们展示所有的 .xml 布局吗?似乎在这两个视图之外发生了一些事情,并且没有足够的上下文来弄清楚是什么。

标签: android xml imageview fragment android-relativelayout


【解决方案1】:

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

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

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

【讨论】:

  • 你是对的。不过这很奇怪,因为它可以在手机上使用。当我复制该布局以解决平板电脑大小的屏幕时,问题就开始了。谢谢!
【解决方案2】:

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

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

【讨论】:

  • 不幸的是它没有帮助..它仍然在左上角。谢谢。
  • 去掉这三个代码 android:layout_centerInParent="true" android:layout_gravity="center" android:gravity="center"
【解决方案3】:

在使用下面属性的RelativeLayout中,我们将ImageView或其他子视图居中。只需在ImageView属性中添加下面的行。

android:layout_centerVertical="true"

【讨论】:

    【解决方案4】:

    删除

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

    然后写

        android:src="@drawable/sign"
    

    代替

        android:background="@drawable/sign"
    

    【讨论】:

      【解决方案5】:

      试试这个

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多