【问题标题】:imageview height as the wrap_content of the shown imageimageview 高度作为所示图像的 wrap_content
【发布时间】:2013-11-09 00:29:13
【问题描述】:

我希望 ImageView 的宽度由父级设置,高度应与纵横比。原因是接下来会显示一个 TextView,我想将它放在 ImageView 下方。

我可以使用正确显示图像

android:layout_width="fill_parent"
android:layout_height="fill_parent"

然而,ImageView 的高度变成了比显示的拉伸图像大得多的父高度。一个想法是让父级垂直变小,但是..我还不知道拉伸后的图像大小。

以下不起作用,因为小图像没有水平填充。

android:layout_width="fill_parent"
android:layout_height="wrap_content"

玩弄

android:layout_height="wrap_content"

对于围绕它的RelativeLayout 没有帮助。也试过 FrameLayout 和 LinearLayout 都失败了。

有什么想法吗?

【问题讨论】:

  • 你尝试过类似developer.android.com/reference/android/widget/…的方法吗?
  • 如果你想要宽高比,你需要使用线性布局的权重。
  • @fgeorgiew 是的,我尝试了所有这些。恐怕没有运气。
  • @Brontok 我试过了,但是 textview 的尺寸很奇怪。

标签: android android-layout android-imageview


【解决方案1】:

有两种情况,如果您的实际图像大小等于或大于您的 ImageView 宽度和高度,那么您可以使用 adjustViewBounds 属性,如果您的实际图像大小小于 ImageView 的宽度和高度,则使用 scaleType 属性在 ImageView 中显示图像根据您的要求。

1.实际图片大小等于或大于ImageView所需的宽度和高度。

<ImageView
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:src="@drawable/ic_launcher"/>

2.实际图片尺寸小于ImageView要求的宽高。

<ImageView
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:scaleType="fitXY"
    android:src="@drawable/ic_launcher"/>

【讨论】:

  • 如果我改成 FrameLayout 就解决了。它不适用于RelativeLayout 或LinearLayout。不过,如果我只是设法将 TextView 附加到 ImageView 下方,则可以。 (android:layout_below 不起作用)- 非常感谢!
  • 如果我将图像+文本放在一个新的 RelativeLayout 中,它会对齐但是图像不再在中心,整个东西都在中心。
  • 我的目标是一张图片1作为背景,居中。然后 image2 在顶部,居中但较小,并且在 image2 文本下方。 image2 和文本由带有动画等的 viewflipper 更改
【解决方案2】:

这是我可以让它工作的唯一方法,让第二个 ImageView 位于中心并且比第一个 ImageView 小,而 TextView 在第二个 ImageView 下。

不幸的是,它在第​​二个 ImageView 上使用了固定的“200dp”图像尺寸,因此在不同尺寸的设备上看起来并不相同。

它还会破坏我的 ViewFlipper,因为我在第二个 ImageView 和 TextView 周围尝试的任何布局都会使它们移动或调整大小。

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <ImageView
        android:id="@+id/imageview1"
        android:contentDescription="@string/desc"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/background" />
    <ImageView
        android:id="@+id/imageview2"
        android:layout_centerInParent="true"
        android:contentDescription="@string/desc"
        android:layout_height="200dp"
        android:layout_width="200dp" 
        android:adjustViewBounds="true"
        android:src="@drawable/image2" />
    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/imageview2"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:gravity="center"
        android:textSize="26sp"
        android:textColor="#333"
        android:background="#fff"
        android:text="this is the text under the image right here"
        />
</RelativeLayout>

【讨论】:

    【解决方案3】:

    您必须将 adjustViewBounds 设置为 true。

    imageView.setAdjustViewBounds(true);
    

    【讨论】:

    • 您可以从界面执行此操作。完美运行
    • 在 xml 中添加 android:adjustViewBounds="true" 查看其他答案-> stackoverflow.com/a/19673468/1815624 @kit 谢谢
    • 您的回答在 5 年后挽救了一条生命,谢谢!
    【解决方案4】:

    所以我不止一次遇到过同样的问题,并且通过查看现有的 stackoverflow 答案已经意识到没有答案可以完美地解释关于解决这个问题的真正困惑。所以给你:

    API 17+

    imageView.setAdjustViewBounds(true);

    或(在 XML 中)

    android:adjustViewBounds="true"

    解决了这个问题,无论图像资源的实际大小如何,它都能正常工作,即它会将您的图像放大或缩小到布局中所需的大小。

    低于 API 17

    低于 API 17 android:adjustViewBounds="true" 仅适用于缩小图像,而不是增长,即如果图像源的实际高度小于您试图在布局中实现的尺寸,wrap_content 将使用较小的高度,而不是根据需要“放大”(放大)图像。

    因此对于 API 17 及更低版本,您别无选择,只能使用自定义 ImageView 来实现此行为。您可以自己编写自定义 ImageView,也可以使用已经完成这项工作的库。

    使用库

    可能有不止一个库可以解决这个问题,其中之一是:

    compile 'com.inthecheesefactory.thecheeselibrary:adjustable-imageview:1.0.0'

    这样使用:

    <com.inthecheesefactory.thecheeselibrary.widget.AdjustableImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:adjustViewBounds="true"
                    android:src="@drawable/your_drawable"/>
    

    使用自定义视图

    除了使用现有库之外,您还可以自己编写自定义视图,例如:

    import android.content.Context;
    import android.graphics.drawable.Drawable;
    import android.util.AttributeSet;
    import android.view.ViewGroup;
    import android.view.ViewParent;
    import android.widget.ImageView;
    
    public class ScalableImageView extends ImageView {
    
        boolean adjustViewBounds;
    
        public ScalableImageView(Context context) {
            super(context);
        }
    
        public ScalableImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public ScalableImageView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        public void setAdjustViewBounds(boolean adjustViewBounds) {
            this.adjustViewBounds = adjustViewBounds;
            super.setAdjustViewBounds(adjustViewBounds);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            Drawable drawable = getDrawable();
            if (drawable == null) {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
                return;
            }
    
            if (adjustViewBounds) {
                int drawableWidth = drawable.getIntrinsicWidth();
                int drawableHeight = drawable.getIntrinsicHeight();
                int heightSize = MeasureSpec.getSize(heightMeasureSpec);
                int widthSize = MeasureSpec.getSize(widthMeasureSpec);
                int heightMode = MeasureSpec.getMode(heightMeasureSpec);
                int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    
                if (heightMode == MeasureSpec.EXACTLY && widthMode != MeasureSpec.EXACTLY) {
                    int height = heightSize;
                    int width = height * drawableWidth / drawableHeight;
                    if (isInScrollingContainer())
                        setMeasuredDimension(width, height);
                    else
                        setMeasuredDimension(Math.min(width, widthSize), Math.min(height, heightSize));
                } else if (widthMode == MeasureSpec.EXACTLY && heightMode != MeasureSpec.EXACTLY) {
                    int width = widthSize;
                    int height = width * drawableHeight / drawableWidth;
                    if (isInScrollingContainer())
                        setMeasuredDimension(width, height);
                    else
                        setMeasuredDimension(Math.min(width, widthSize), Math.min(height, heightSize));
                } else {
                    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
                }
            } else {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            }
        }
    
        private boolean isInScrollingContainer() {
            ViewParent parent = getParent();
            while (parent != null && parent instanceof ViewGroup) {
                if (((ViewGroup) parent).shouldDelayChildPressedState()) {
                    return true;
                }
                parent = parent.getParent();
            }
            return false;
        }
    }
    

    ...您将按如下方式使用 (XML):

    <com.YOUR_PACKE_NAME.ScalableImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/your_drawable" />
    

    【讨论】:

      猜你喜欢
      • 2016-11-06
      • 1970-01-01
      • 1970-01-01
      • 2019-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多