【问题标题】:Removing space above and below ImageView in Android在Android中删除ImageView上方和下方的空间
【发布时间】:2016-03-09 08:54:37
【问题描述】:

您好,我需要帮助,因为我正在 在我的 android xml 文件中添加一个 ImageView,并且我在其上方和下方得到了一个 额外空间 我试图查看以下选项我可以用来删除它其中之一是添加android:adjustViewBounds="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"
    tools:context="com.example.fred.yebo.MainFragment">

  <ImageView
      android:id="@+id/yeboAdd"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:src="@drawable/dice"

      />

</RelativeLayout>

【问题讨论】:

  • android:scaleType="fitXY" 但它可以使您的图像拉伸。
  • 它不起作用它会拉伸图像并降低分辨率
  • 通常有两种解决方案,第一个是 fitXy,它会拉伸你的图像,第二个是你应该使用 wrap_content 宽度而不是匹配父项
  • 我将代码更改为 wrap_content ,但图像仍然看起来不错@VivekMishra
  • 除非硬编码图像的宽度和高度,否则无法让它看起来很好

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


【解决方案1】:

试试这个属性:

android:adjustViewBounds="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"
    tools:context="com.example.fred.yebo.MainFragment">

    <ImageView
        android:id="@+id/yeboAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/cal" />

</RelativeLayout>

check this answer

【讨论】:

  • 我已经尝试过了,但它会删除条形图并将图像置于顶部
  • 你可能做错了什么,否则这工作正常。你现在面临什么问题?
  • 我面临的问题是应用android:adjustViewBounds="true" 会删除顶栏
  • 您检查过我的回答中提到的链接吗? @user3551487
  • 这是正确的方法。当您调整图像大小时,将此属性添加到 imageview 后将解决。
【解决方案2】:

您可以尝试使用 imageView 的 scaleType 属性

android:scaleType="fitXY"

这条线会根据容器的大小缩放图像。您可以尝试一些 scaleType 属性,这将更适合您的情况。希望对您有所帮助!

【讨论】:

  • 我认为您必须为您的图像视图设置一些宽度和高度值,或者至少将宽度设置为 wrap_content,因为您这样做的方式会使图像看起来不太好并且会被拉伸。跨度>
【解决方案3】:
<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="com.example.fred.piku.MainFragment">

    <ImageView
        android:id="@+id/pikuAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/dice" />

</RelativeLayout>

【讨论】: