【问题标题】:Empty space in RelativeLayout相对布局中的空白空间
【发布时间】:2015-09-30 19:58:47
【问题描述】:

我尝试使用RelativeLayout 将图像覆盖在另一个图像上,但出现了不必要的空白。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#263238"
android:orientation="vertical">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="12dp"
    android:paddingRight="12dp"
    android:paddingTop="12dp"
    android:gravity="top">

    <ImageView
        android:id="@+id/heroImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/test3"
        android:layout_alignParentTop="true"
        android:scaleType="fitStart" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/corner" />

</RelativeLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFF"
    android:id="@+id/linearlayout_hero">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hero Appearance"/>
</LinearLayout>

你可以在Wolverine的图片和文字之间看到它。

【问题讨论】:

  • 您能定义“不必要的空白空间”是什么意思吗?您提供了 12dp 的填充,并且您的图像的宽高比与其所在的帧不同。使图像适合空间的唯一方法是拉伸或剪裁左右两侧。

标签: android android-layout android-imageview android-relativelayout


【解决方案1】:

将此添加到您的 ImageView

android:scaleType="fitStart"
android:adjustViewBounds="true"

【讨论】:

  • 它在空间上拉伸图像,但我需要图像保持正方形。
  • 您认为图像应该如何显示?剪裁左右两边?
  • 在这种情况下,您可以使用框架布局,在您的情况下,图像的两侧有空格,因此您需要删除这些空格或使用框架布局
  • @Aakash ,我把它改成FrameLayout,但仍然有相同的空白空间
  • @IgorSkryl 编辑了我的答案,请检查,现在应该可以使用
【解决方案2】:

用这个块替换你的底部线性布局

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="#FFF"
    android:id="@+id/linearlayout_hero">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hero Appearance"/>
</LinearLayout>

【讨论】:

  • @Ravie Gadipudi ,我应该用你的代码更改我的代码吗?如果是,它没有帮助:(
【解决方案3】:

你可以像这样把 LinearLayout 放在 RelativeLayout 里面:

    <LinearLayout
        android:id="@+id/linearlayout_hero"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/heroImage"
        android:background="#FFF">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hero Appearance" />
    </LinearLayout>

【讨论】:

    【解决方案4】:

    我正在尝试回答你的问题。

    我认为这是因为您的图像(英雄/角落)底部有一个空白区域,因为RelativeLayout的宽度和高度是包装内容,所以RelativeLayout适合图像。

    请检查您的图片:)

    【讨论】:

      猜你喜欢
      • 2018-06-02
      • 2018-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多