【问题标题】:My ImageView is changing size depending on Image (Android)我的 ImageView 正在根据 Image (Android) 改变大小
【发布时间】:2013-07-21 02:52:30
【问题描述】:

我正在为 Android 编写一个应用程序,但我遇到了下一个问题:

我需要通过 Http 将下载的图像显示到包含 ImageView 和 Text 的列表视图中。两者或它们都包含在 LinearLayout 中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/imagenEvento"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:scaleType="fitXY"
    android:adjustViewBounds="true"
    android:paddingBottom="2dp"     
    android:contentDescription="@string/contenidoImagen"       

    />


<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:padding="10dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/titulo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="17sp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/sitio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/fecha"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="12sp"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>

我以不同的大小动态加载图像,但是当我加载其中一些图像时,我的 ImageView 的大小受到影响,它不会尊重我的高度和宽度声明。

我尝试确定 MAX 和 MIN 尺寸,我在代码中定义了布局参数,确定了底部填充,调整了位图但没有任何效果。

在下一张图片中,黄色圆圈显示相同的空间,红色圆圈显示 ImageView 和行底部之间的不同空间 http://imageshack.us/photo/my-images/32/csww.jpg/

我错过了什么?

【问题讨论】:

  • 不,它没有用,我一直得到相同的结果
  • 但是 ImageVIew 保持他的大小,正确的 LinearLayout 包含 TextViews 正在根据行而变化
  • @MarcinGawel ImageView 我认为正在改变大小。 TextViews 会根据行发生变化,没错,但它不应该影响 ImageView 的大小
  • 其实不然,ImageSize 还是一样的,但是 ImageSize 和底线之间的差距正在改变,因为整个 LinearLayout 改变了他的大小。将 android:layout_gravity="center" 放到 ImageView 中,它将位于中心。看看这个link
  • @MarcinGawel 你是对的,LinearLayout 会根据行而变化

标签: android image imageview image-size


【解决方案1】:

你可以试试这段代码。如果这个功能有效,那么您可以使用 relativelayout 并将所有图像视图和文本放在一个布局中,并尝试在该相对布局中进行调整

<ImageView
    android:id="@+id/imagenEvento"
    android:layout_width="80dp"
    android:layout_height="fill_parent"
    android:scaleType="fitXY"
    android:adjustViewBounds="true"
    android:paddingBottom="2dp"     
    android:contentDescription="@string/contenidoImagen"       

    />

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:layout_alignParentTop="true" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="16dp"
            android:layout_toRightOf="@+id/imageView1"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_below="@+id/textView1"
            android:text="Content Name" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView2"
            android:layout_below="@+id/textView2"
            android:text="29/02/2009" />
    </RelativeLayout>

</RelativeLayout>

【讨论】:

  • 它工作得很好,我只是改变了 android:layout_width="80dp" android:layout_height="80dp",唯一的问题是 ImageViews 正在改变它们的大小,我不希望这种情况发生
【解决方案2】:

如果您想要特定尺寸的 ImageView,为什么要使用 android:adjustViewBounds? 使用 android:adjustViewBounds 你的 ImageView 将改变它的大小以保持图像的纵横比。

如果您想以真正的 80x80 显示图像 - 删除 android:adjustViewBounds 并将 scaleType 更改为 centerCrop。这样,图像将尽可能缩小,因此两个尺寸都将触及 ImageView 边界,其余的将被裁剪。

如果您希望图像完全可见,或者使用 scaleType="centerInside"(100% 保证看起来更丑:D)

不管怎样,android:adjustViewBounds 都必须走。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-29
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    • 2023-02-19
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多