ImageView – 高度匹配宽度?

我有一个imageview。 我想它的宽度是fill_parent。 我希望它的高度是任何宽度最终成为。 例如:

<ImageView android:layout_width="fill_parent" android:layout_height="whatever the width ends up being" /> 

在布局文件中可以这样做,而不必创build自己的视图类?

谢谢

也许这会回答你的问题:

 <ImageView android:id="@+id/cover_image" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scaleType="fitCenter" android:adjustViewBounds="true" /> 

您可以忽略此特定情况的scaleType属性。

更新:2017年9月14日

根据下面的评论,从Android支持库26.0.0开始,百分比支持库已被弃用。 这是做到这一点的新方法:

 <android.support.constraint.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:layout_width="wrap_content" android:layout_height="0dp" app:layout_constraintDimensionRatio="1:1" /> </android.support.constraint.ConstraintLayout> 

看到这里

推荐使用:

根据Android开发者的这篇文章 ,你现在需要做的就是在PercentRelativeLayout或者PercentFrameLayout中包装任何你想要的东西,然后指定它的比例,就像这样

 <android.support.percent.PercentRelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView app:layout_widthPercent="100%" app:layout_aspectRatio="100%"/> </android.support.percent.PercentRelativeLayout> 

这可以使用LayoutParams来dynamic设置视图高度,一旦你知道在运行时的视图宽度。 您需要使用Runnable线程才能在运行时获取视图宽度,否则在知道视图宽度之前,您将尝试设置高度,因为尚未绘制布局。

我如何解决我的问题的例子:

 final FrameLayout mFrame = (FrameLayout) findViewById(R.id.frame_id); mFrame.post(new Runnable() { @Override public void run() { RelativeLayout.LayoutParams mParams; mParams = (RelativeLayout.LayoutParams) mFrame.getLayoutParams(); mParams.height = mFrame.getWidth(); mFrame.setLayoutParams(mParams); mFrame.postInvalidate(); } }); 

LayoutParams必须是您的视图所在的父视图的types。我的FrameLayout位于xml文件的RelativeLayout内部。

  mFrame.postInvalidate(); 

被调用来强制视图重绘,而在一个单独的线程比UI线程

在这里,我做了什么我有一个ImageButton总是有一个宽度等于其高度(并避免在一个方向愚蠢的空边距…我认为是一个错误的SDK …):

我定义了一个从ImageButton扩展的SquareImageButton类:

 package com.myproject; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.widget.ImageButton; public class SquareImageButton extends ImageButton { public SquareImageButton(Context context) { super(context); // TODO Auto-generated constructor stub } public SquareImageButton(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public SquareImageButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } int squareDim = 1000000000; @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ super.onMeasure(widthMeasureSpec, heightMeasureSpec); int h = this.getMeasuredHeight(); int w = this.getMeasuredWidth(); int curSquareDim = Math.min(w, h); // Inside a viewholder or other grid element, // with dynamically added content that is not in the XML, // height may be 0. // In that case, use the other dimension. if (curSquareDim == 0) curSquareDim = Math.max(w, h); if(curSquareDim < squareDim) { squareDim = curSquareDim; } Log.d("MyApp", "h "+h+"w "+w+"squareDim "+squareDim); setMeasuredDimension(squareDim, squareDim); } } 

这是我的xml:

 <com.myproject.SquareImageButton android:id="@+id/speakButton" android:layout_width="wrap_content" android:layout_height="match_parent" android:scaleType="centerInside" android:src="@drawable/icon_rounded_no_shadow_144px" android:background="#00ff00" android:layout_alignTop="@+id/searchEditText" android:layout_alignBottom="@+id/searchEditText" android:layout_alignParentLeft="true" /> 

奇迹般有效 !

你不能单独做布局,我试过了。 我写了一个非常简单的类来处理它,你可以在github上查看。 SquareImage.java它是一个更大的项目的一部分,但没有一点复制和粘贴不能修复(Apache 2.0许可)

基本上你只需要设置高度/宽度等于其他维度(取决于你想要缩放的方式)

注意:您可以使用scaleType属性将其设置为不带自定义类的scaleType但是视图的边界会超出可见图像,如果您将其他视图放在其附近,则会造成问题。

要将ImageView设置为屏幕的一半,需要将以下内容添加到ImageView的XML中:

 <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" android:scaleType="fitXY" android:adjustViewBounds="true"/> 

然后设置高度等于这个宽度,你需要在代码中完成。 在GridView适配器的getView方法中,将ImageView高度设置为等于其测量的宽度:

 mImageView.getLayoutParams().height = mImageView.getMeasuredWidth(); 

对于现在过去的人来说,在2017年,通过使用ConstraintLayout来实现你想要的新的最佳方式是这样的:

 <ImageView android:layout_width="0dp" android:layout_height="0dp" android:scaleType="centerCrop" app:layout_constraintDimensionRatio="1:1" /> 

并且不要忘记根据布局的需要,在所有四个方向上添加约束条件。

使用ConstraintLayout构build响应式UI

此外,到目前为止,PercentRelativeLayout已被弃用(请参阅Android文档 )。

以下是我如何解决这个问题:

 int pHeight = picture.getHeight(); int pWidth = picture.getWidth(); int vWidth = preview.getWidth(); preview.getLayoutParams().height = (int)(vWidth*((double)pHeight/pWidth)); 

预览 – imageView宽度设置为“match_parent”和scaleType为“cropCenter”

图片 – 在imageView src中设置的位图对象。

这对我来说很好。

在Android 26.0.0 PercentRelativeLayout已被弃用 。

现在解决这个问题的最好的方法是用这样的ConstraintLayout

 <android.support.constraint.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:layout_width="match_parent" android:layout_height="0dp" android:scaleType="centerCrop" android:src="@drawable/you_image" app:layout_constraintDimensionRatio="1:1"/> </android.support.constraint.ConstraintLayout> 

这里是关于如何将ConstraintLayout添加到您的项目的教程 。

我不认为你有什么办法可以在XML布局文件中做到这一点,我不认为android:scaleType属性会像你想要的那样工作。
唯一的方法是以编程方式进行。 您可以将宽度设置为fill_parent,并可以将屏幕宽度作为View的高度,也可以使用View.getWidth()方法。

ImageView的“scaleType”function可以帮助你。

这段代码将保持高宽比,并将图像放置在顶部:

 android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="fitStart" 

这是一个很好的post,展示了使用scaleType的可能性和外观。

ImageView scaleType样本

我无法得到David Chu的回答为RecyclerView项目工作,并找出我需要将ImageView约束到父级。 将ImageView宽度设置为0dp并将其开始和结束限制为父级。 我不确定在某些情况下将宽度设置为wrap_contentmatch_parent效果,但是我认为这是让ConstraintLayout的子项填充其父项的更好方法。

 <android.support.constraint.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintDimensionRatio="1:1"/> </android.support.constraint.ConstraintLayout>