简体   繁体   English

ImageView使用父布局高度

[英]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" />

As described here ,you can get the height of the layout at runtime. 如上所述在这里 ,你可以在运行时布局的高度。 Get it and set it to imageview's width. 获取它并将其设置为imageview的宽度。 And the image will become exact square. 并且图像将成为精确的正方形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM