Java 调整行大小时,用视图填充Listview行高

Java 调整行大小时,用视图填充Listview行高,java,android,xml,android-layout,android-listview,Java,Android,Xml,Android Layout,Android Listview,我面临着一个看似简单的问题,但经过几个小时的案件,我现在无法解决它。我正在尝试创建一个列表视图,其中的行包含一个ImageView,该视图根据ImageView的图像重新调整大小。这是通过对ImageView进行子类化来实现的,并给它一个要遵循的高度比,因为我在加载图像之前就知道了图像的大小: ImageViewonMeasure方法的自定义子类: @Override protected void onMeasure(int widthMeasureSpec, int heightMea

我面临着一个看似简单的问题,但经过几个小时的案件,我现在无法解决它。我正在尝试创建一个
列表视图
,其中的行包含一个
ImageView
,该视图根据
ImageView的
图像重新调整大小。这是通过对
ImageView
进行子类化来实现的,并给它一个要遵循的高度比,因为我在加载图像之前就知道了图像的大小:

ImageView
onMeasure
方法的自定义子类:

@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mHeightRatio > 0.0) {
            // set the image views size
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = (int) (width * mHeightRatio);
            setMeasuredDimension(width, height);
        }
        else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
在适配器中,我设置
ImageView
的比率:

holder.couponImage.setHeightRatio(359.0/1080.0);
问题是,我还希望在
图像视图
上覆盖一个半透明背景的
相对视图
,并包含
文本视图
。问题是,当
ImageView
重新zises覆盖的
RelativeLayout
没有时,下图中的白色是
RelativeLayout

行XML如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <se.bdms.babydirect.views.DynamicHeightImageView
        android:id="@+id/couponImage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:src="@drawable/coupon_01"/>

    <RelativeLayout
        android:id="@+id/relRedeemedOverlay"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/almostWhiteTransparent"
        android:gravity="center"
        >

        <se.bdms.babydirect.views.TextViewMega
            android:id="@+id/lblRedeemed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextField"
            android:textColor="@color/headlineGray"
            android:layout_centerInParent="true"
            android:textSize="24sp"/>

        </RelativeLayout>

</RelativeLayout>

我尝试了多种解决方案来解决这一问题:和。 我已经成功地使
相对视图
与ImageView大小相同,方法与
ImageView
子类化的方式相同,但是
文本视图
仍然没有垂直居中,并且保持在顶部,就像什么都没有发生一样

我想让RelativeLayout知道行高度已更改,并填充行,然后TextView根据新高度将自身居中

有人知道该怎么办吗?
非常感谢您的帮助

我认为问题在于你需要告诉覆盖层它与图像的高度相同。你可以这样做,因为父母已经是一个亲戚了。我显示的是将子RelativeLayout与ImageView对齐,但您也可以使用alignParentTop=“true”和alignParentBottom=“true”。另外,我认为最好的方法是,如果父视图具有高度换行内容,并且两个子视图都是匹配父视图

一旦覆盖层达到项目的全高,centerInParent应将其置于覆盖层的中心(该覆盖层已经处于中心位置)



让我知道这是否有效。

您发出的信号不是1个问题,而是3个问题:“当ImageView重新zises时,重叠的RelativeLayout不存在”,“白色也应该覆盖这一点”和“TextView仍然没有居中”,对吗?@shkschneider RelativeLayout是白色的,因此这只是视觉上解释的同一件事。TextView的问题是,如果我以子类ImageView的方式将RelativeLayout子类化,TextView将不会记录任何内容并保持在行的顶部。对我来说,最好的解决方案是,如果有一种方法可以让RelativeLayout填充该行,然后文本视图就会出现。编辑的问题。嗨,谢谢你的回答。这几乎奏效了,这次RelativeLayout覆盖确实填充了行,但TextView没有居中。它会粘在顶部。请尝试使TextView的布局高度和布局宽度与父级匹配,并将android:gravity(非布局重力)=“true”。No sorry不起作用。奇怪的是,你的原始答案在我的旧API10设备上运行。但不是在我当前的API 19 S4上。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <se.bdms.babydirect.views.DynamicHeightImageView
        android:id="@+id/couponImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:src="@drawable/coupon_01"/>

    <RelativeLayout
        android:id="@+id/relRedeemedOverlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignTop="@id/couponImage"
        android:layout_alignBottom="@id/couponImage"
        android:background="@color/almostWhiteTransparent"
        android:gravity="center"
        >

        <se.bdms.babydirect.views.TextViewMega
            android:id="@+id/lblRedeemed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextField"
            android:textColor="@color/headlineGray"
            android:layout_centerInParent="true"
            android:textSize="24sp"/>

        </RelativeLayout>

</RelativeLayout>