【问题标题】:How can I get an ImageView to fill parent on a relative layout?如何让 ImageView 在相对布局上填充父级?
【发布时间】:2018-03-19 21:52:14
【问题描述】:

我正在尝试拉伸 300x300 图像以填充父级作为示例布局的背景。然而,它似乎最多可以达到它的最大尺寸作为一个完美的正方形。这是我的代码。

<ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/app_background"
        android:scaleType="fitCenter"
        android:adjustViewBounds="true"/>

应用的顶部和底部仍然有空白,我想消除它。任何和所有解决这个困境的办法都将不胜感激。enter image description here

【问题讨论】:

  • 为什么不将父母的android:background 设置为@drawable/app_background编辑:正如@Chisko 建议的那样,您也可以在ImageView 中使用android:scaleType="fitXY"
  • 玩弄android:scaleTypefitCenter 当前选项负责“完美”的事情
  • 天哪,谢谢斯特凡·戈卢博维奇!如果没有你的帮助,我永远不会知道这件事。

标签: android xml imageview fill-parent


【解决方案1】:

使用 ma​​tch_parent

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/app_background"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true"/>

【讨论】:

  • fill_parentmatch_parent 相同。
  • 当我尝试使用它时它不起作用,但我找到了另一个解决方案。谢谢。
【解决方案2】:

为我工作:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="org.jtb.stretchtest.MainActivity">

  <ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitCenter"
    android:src="@drawable/test"/>
</RelativeLayout>

【讨论】:

    【解决方案3】:

    检查父元素是否有填充。

    将父窗口部件的 Android padding 设置为 0dp 例如: android:padding = "0dp"

    【讨论】:

      【解决方案4】:

      这对我有用:

      <ImageView
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:scaleType="fitXY"
              android:src="@drawable/app_background"/>
      

      【讨论】:

        猜你喜欢
        • 2012-07-15
        • 2021-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多