Android ImageView使用父布局高度
我的问题和你的一样,但答案并不能解决问题。我对Android ImageView使用父布局高度,android,layout,imageview,scale,fill-parent,Android,Layout,Imageview,Scale,Fill Parent,我的问题和你的一样,但答案并不能解决问题。我对ImageView进行了子类化,并覆盖了onMeasure,如下所示: @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ this.setMeasuredDimension(this.getMeasuredHeight(), this.getMeasuredHeight()); super.onMeasure(widthM
ImageView
进行了子类化,并覆盖了onMeasure
,如下所示:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
this.setMeasuredDimension(this.getMeasuredHeight(), this.getMeasuredHeight());
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
这不会改变任何事情,如果我尝试使用链接帖子答案中的代码,ImageView
会变得不可见(0宽度/高度),因为heightMeasureSpec是0,MeasureSpec.getSize(heightMeasureSpec)
也是0
目标是使ImageView
与父视图一样高,并且宽度相同(因为图像是正方形的)
编辑:这是布局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" />
无需对“ImageView”进行子类化。就这么做吧
<ImageView
android:id="@+id/slider_image"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="fitXY" />
在ImageView中使用以下字段
android:scaleType="fitXY"
android:adjustViewBounds="true"
如上所述,您可以在运行时获取布局的高度。获取它并将其设置为imageview的宽度。图像将变成完全正方形。为什么要覆盖测量?您不能在XML中仅使用
layout\u height=“match\u parent”
“match\u parent”和“wrap\u content”给出相同的结果,这不会填充列表项的高度。我通过使用alignTop和alignBottom实现了这一点。实际上这不起作用。上面我使用的是centerCrop scaletype,它可以剪切图像的左右边缘,fitXY使图像宽度缩小并看起来失真。我不想设置这样的宽度,我希望它与高度匹配。我想问题是我使用的是alignBottom和alignTop。height=“match\u parent”在这里似乎不起作用,大小与使用wrap\u内容相同。