[英]ImageView use parent layout height
I'm having the same problem as this question but the answers there don't solve the problem. 我遇到了与这个问题相同的问题,但那里的答案并没有解决问题。 I've subclassed ImageView
, and overriden onMeasure
like so: 我已经将ImageView
子类化,并覆盖onMeasure
如下所示:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
this.setMeasuredDimension(this.getMeasuredHeight(), this.getMeasuredHeight());
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
This doesn't change anything, and if I try using the code from the answers of the linked post, the ImageView
becomes invisible (0 width/height) because the heightMeasureSpec is 0, and MeasureSpec.getSize(heightMeasureSpec)
is also 0
. 这不会改变任何东西,如果我尝试使用链接帖子的答案中的代码, ImageView
将变为不可见(0宽度/高度),因为heightMeasureSpec为0,而MeasureSpec.getSize(heightMeasureSpec)
也为0
。
The goal is to have the ImageView
be as tall as the parent view, and have the width be the same (as the image is square). 目标是使ImageView
与父视图一样高,并且宽度相同(图像为方形)。
Edit: here is my ImageView in the layout XML: 编辑:这是我在布局XML中的ImageView:
<ImageView
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/bottomtext"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/toptext"
android:adjustViewBounds="true"
android:padding="4dp"
android:scaleType="centerCrop" />
use following field in ImageView 在ImageView中使用以下字段
android:scaleType="fitXY"
android:adjustViewBounds="true"
No need to subclass the `ImageView'. 无需子类化`ImageView'。 Just do this 就这样做吧
<ImageView
android:id="@+id/slider_image"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="fitXY" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.