带有 LayoutParams match_parent的图像视图在运行时更改父级大小时不匹配父级



我有一个imageview,我想填充它的父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>

好的,我相信你已经知道了,但为了彻底起见,我认为我应该提到设置相对布局背景的最简单方法是直接在背景字段:

<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的背景设置为亮橙色。这样你就知道它们在哪里了:-)

最新更新