ImageView - 高度匹配宽度?

use*_*701 152 android

我有一个imageview.我希望它的宽度为fill_parent.我希望它的高度无论宽度最终如何.例如:

<ImageView
  android:layout_width="fill_parent"
  android:layout_height="whatever the width ends up being" />
Run Code Online (Sandbox Code Playgroud)

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

谢谢

dav*_*aya 152

更新: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>
Run Code Online (Sandbox Code Playgroud)

看到这里

推荐使用:

根据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>
Run Code Online (Sandbox Code Playgroud)

  • 这个答案的链接提供了Google提供的最优雅的解决方案.就我而言,无论原始图像长宽比是多少,我都希望图像以16:9的比例显示.效果很好.谢谢! (7认同)
  • 别忘了在依赖项中添加'compile com.android.support:percent:23.3.0' (5认同)

小智 93

也许这会回答你的问题:

<ImageView
    android:id="@+id/cover_image"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true" />
Run Code Online (Sandbox Code Playgroud)

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

  • 这仅适用于图像自身为方形的情况 (39认同)
  • 是的,那没用 (11认同)
  • 这不是无用的.这适用于我的用法,我在标题栏上有一个图标我想缩放以填充高度,同时保持正方形. (4认同)
  • 解决了我的问题. (3认同)

dre*_*ees 30

这可以使用LayoutParams在运行时知道视图宽度后动态设置视图高度.您需要使用Runnable线程才能在运行时获取Views宽度,否则您将在知道View的宽度之前尝试设置Height,因为尚未绘制布局.

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

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();
        }
    });
Run Code Online (Sandbox Code Playgroud)

LayoutParams必须是视图所在的父视图的类型.我的FrameLayout位于xml文件中的RelativeLayout内.

    mFrame.postInvalidate();
Run Code Online (Sandbox Code Playgroud)

被调用以强制视图在与UI线程不同的单独线程上重绘

  • 很棒的答案,尤其是Runnable,谢谢! (4认同)

Reg*_*_AG 13

在这里,我做了一个ImageButton,它的宽度总是等于它的高度(并避免在一个方向上的愚蠢的空边距......我认为这是一个SDK的bug ...):

我定义了一个从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);

    }

}
Run Code Online (Sandbox Code Playgroud)

这是我的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"
           />
Run Code Online (Sandbox Code Playgroud)

奇迹般有效 !


Sha*_*ten 11

我无法得到David Chu的回答来为RecyclerView项目工作,并想出我需要将ImageView约束到父级.将ImageView宽度设置为0dp并将其开始和结束约束到父级.我不确定在某些情况下是设置宽度wrap_content还是match_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>
Run Code Online (Sandbox Code Playgroud)


Cha*_*raM 11

如果您的图像视图在约束布局内,您可以使用以下约束来创建方形图像视图,确保使用 1:1 来制作方形

<ImageView
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:id="@+id/ivImageView"
    app:layout_constraintDimensionRatio="1:1"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"/>
Run Code Online (Sandbox Code Playgroud)


Mat*_*gan 6

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

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:scaleType="fitXY"
    android:adjustViewBounds="true"/>
Run Code Online (Sandbox Code Playgroud)

要将高度设置为等于该宽度,您需要在代码中执行此操作。在适配器getView的方法中GridView,将ImageView高度设置为其测量的宽度:

mImageView.getLayoutParams().height = mImageView.getMeasuredWidth();
Run Code Online (Sandbox Code Playgroud)


Seb*_*der 6

在 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>
Run Code Online (Sandbox Code Playgroud)


这是有关如何添加到项目的教程ConstraintLayout


smi*_*324 5

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

基本上你只需要设置高度/宽度等于另一个尺寸(取决于你想要缩放它的方式)

注意:您可以使用该scaleType属性使其成为正方形而不使用自定义类,但视图的边界超出可见图像范围,如果您将其他视图放在其附近,则会出现问题.