我面临着一个看似简单的问题,但经过几个小时的处理后,现在我无法弄清楚。我正在尝试创建一个 ListView
,其中包含一个 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
上以 RelativeLayout
的形式覆盖,具有半透明背景并包含 TextView
。问题是,当 ImageView
调整覆盖的 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>
我尝试了多种解决方案来解决此问题:this和 this 。
我成功地通过子类化 RelativeLayout
与 ImageView 的大小相同,就像子类化 ImageView
一样,然后 TextView
是仍然没有垂直居中并停留在顶部,就像什么都没发生一样。
我想让RelativeLayout知道行高已经改变并填充行,然后TextView根据新的高度居中。
有人知道该怎么办吗? 任何帮助是极大的赞赏!
最佳答案
我认为问题在于您需要告诉叠加层它与图像的高度相同。您将能够做到这一点,因为父级已经是relativelayout。我展示了将子RelativeLayout 与ImageView 对齐,但您也可以使用alignParentTop="true"和alignParentBottom="true"。另外,我认为最好的是父 View 的高度为wrapp_content,并且两个 subview 都是match_parent。
一旦您的覆盖层达到项目的完整高度,您的 centerInParent 应该将其置于覆盖层的中心(它已经是这样了)。
<?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>
让我知道这是否有效。
关于java - 调整行大小时用 View 填充 ListView 行高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27290162/