【问题标题】:aligning 2 imageViews in a RelativeLayout在 RelativeLayout 中对齐 2 个图像视图
【发布时间】:2013-08-18 00:13:48
【问题描述】:

我在相对布局中对齐 2 个图像视图时遇到问题:

这 2 个图像需要靠近才能在没有空间的情况下相互连接。像这样:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background_portrait"
android:orientation="vertical" >

<!-- header -->

<ImageView
    android:id="@+id/header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:contentDescription="@string/desc"
    android:src="@drawable/header" />

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

    <ImageView
        android:id="@+id/dia1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:contentDescription="@string/desc"
        android:src="@drawable/dia1" />

    <ImageView
        android:id="@+id/dia2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/dia1"
        android:contentDescription="@string/desc"
        android:src="@drawable/dia2" />

</RelativeLayout>

我做错了什么?

更新 dia1 是: dia2 是:

【问题讨论】:

  • 你也可以上传你的图片吗?如果你的图像是在一条直线上裁剪的......那么两个可能的问题可能是...... 1.如果你的drawables有额外的透明空间,正如我在第一张图片中看到的那样,需要删除它。 2. 如果裁剪后问题仍然存在,那么最好将 Imageview 背景设置为不是 src。
  • 您能否用整个布局更新您的问题,并更好地指出哪个drawable 是哪个?

标签: java android xml layout android-relativelayout


【解决方案1】:

从您发布的第一张图片来看,底部图像似乎有很多透明的“填充”(不是实际的填充,只是可见部分和上限之间的透明图像像素)。

在我看来,您应该将RelativeLayout 与上下图像的裁剪版本结合起来。

否则,请考虑检查 this SO 线程关于重叠图像。

【讨论】:

  • absolutelayout 已弃用 :)
  • @user1104939 我真是太傻了。感谢您的提醒。 (编辑了我的帖子)
  • 哦,顺便说一句,两张图片都被完美裁剪,但由于某种原因,视图变得很大:)
【解决方案2】:

您的问题并没有解释太多问题。因此,您可以添加单个图像。我的意思是合并之前的 2 张图像并将其用作 1。

【讨论】:

  • 不,他们需要分开以备将来使用:)
  • 在使用这 2 张图片之前,请确认它们没有白色边框。其次,不要使用 match_parent 作为 layout_width,而是尝试使用 wrap_content。这样可以确保没有额外的填充。
  • 两张图片都被裁剪了,wrap_content 没有改变:S