【问题标题】:ImageView with LayoutParams match_parent doesn't match parent when parent size changes at runtime当父级大小在运行时更改时,带有 LayoutParams match_parent 的 ImageView 与父级不匹配
【发布时间】:2014-08-24 08:56:12
【问题描述】:

我有一个图像视图,我想填充它的父级 RelativeLayout 以充当背景图像。

在相关布局中的其他视图展开之前,图像的大小是正确的,从而扩大了整个布局的大小(在我的情况下只是高度)(即:来自互联网的大字符串已加载到 textView )。然后图像视图不会增长以继续匹配父级。这是预期的行为吗?如果是,我们如何绕过它?

这是我的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/parentLayout">
<my.BlurredImageView
    android:scaleType="centerCrop"
    android:id="@+id/profile_bg"
    ImageUrl= "Avatar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
 />
 <LOTS AND LOTS OF OTHER VIEWS>

【问题讨论】:

  • 你怎么知道“图像视图没有增长”?
  • 起初我猜它没有,因为添加项目后,图像仅覆盖了相对布局的 3/4。然后我在Android Monitor Layout View中查看,看到了对象的边界

标签: android android-layout android-relativelayout


【解决方案1】:

好的,我相信您已经知道这一点,但为了彻底起见,我想我应该提一下,设置相对布局背景的最简单方法是直接在背景字段中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="drawable file or file from internet you load programatically"
    android:id="@+id/parentLayout">

现在回答你的问题。如果出于某种原因您确实需要一个 imageView 作为背景,则使用宽度和高度作为 0dp,因为您在所有 4 个边上都与父级对齐。看看这是否有所作为:

<my.BlurredImageView
    android:scaleType="centerCrop"
    android:id="@+id/profile_bg"
    ImageUrl= "Avatar"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    />

要检查图像视图的实际大小,以及它是否填充了您的相对布局,最实用的方法是使RelativeLayout 的背景为亮绿色,而imageView 的背景为亮橙色。然后你总是知道他们在哪里:-)

【讨论】:

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