【问题标题】:Android - ImageView from URL won't fill_parent in ListViewAndroid - 来自 URL 的 ImageView 不会在 ListView 中填充_parent
【发布时间】:2013-05-09 12:59:37
【问题描述】:

我有一个 JSON,我正在解析 JSON。它会在列表视图中显示文本和图像。 除了一件事,一切都很好。 我试图让 ImageView 以 fill_parent 作为宽度,将 wrap_content 作为高度。这不起作用..

这是我的布局:

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

<TextView
    android:id="@+id/id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/id"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/created_at"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/title"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/loves"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/created_at"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/comments"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/loves"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/hypes"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/comments"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/hypes"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/byline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/name"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<ImageView
    android:id="@+id/iv_flag"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/byline" />

如果需要,这是我在 Listview 中显示图像的 Java: http://pastebin.com/N0j5gjPE

谁能帮帮我?

【问题讨论】:

  • 您需要图像是其父级的全宽吗?
  • @Muhannad 是的,图像的高度不同,因此宽度必须填充其父宽度,高度取决于原始比例
  • 尝试将图像视图的 scaleType 设置为 fitXY

标签: android json image listview


【解决方案1】:

试试这个:

     <ImageView
        android:id="@+id/iv_flag"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"/>

来自文档:

android:adjustViewBounds
Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.

尝试将scaleType="fitCenter" 更改为"centerInside"

如果这不起作用,试试这个自定义 ImageView。

public class AspectRatioImageView extends ImageView {

public AspectRatioImageView(Context context) {
    super(context);
}

public AspectRatioImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public AspectRatioImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = width * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth();
    setMeasuredDimension(width, height);
}

}

并将 ImageView 替换为

     <com.package.AspectRatioImageView
    android:id="@+id/iv_flag"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"/>

【讨论】:

  • 这行得通。比例很好,但并没有填满整个宽度。我改变了 android:widht="fill_parent" 但这没有任何帮助..
  • 好的,告诉我什么样的问题?
  • 因为我从 JSON 加载图像并且它们在线我正在使用 loopj.com/android-smart-image-view 这已经是一个自定义 ImageView 所以你知道我怎样才能让它工作吗? @Chronos
  • 如果你有图书馆源代码。您可以在 onMesure 方法上添加此行: int width = MeasureSpec.getSize(widthMeasureSpec); int 高度 = 宽度 * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth(); setMeasuredDimension(宽度, 高度);
【解决方案2】:

你可以试试把这个属性加到 imageView 上

android:scaleType="fitXY"

看起来像

 <ImageView
        android:id="@+id/iv_flag"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:src="@drawable/abs__ab_bottom_solid_light_holo"

        android:scaleType="fitXY"

        android:layout_below="@+id/byline" />

【讨论】:

  • 它已经取得了进展 :D 宽度现在正在工作,除了高度.. 在这里,看看:i.imgur.com/XswW73R.jpg
  • 这是因为你设置了 wrap_content 所以它会按原样包裹图像高度,所以尝试使用另一个高度大于现有图像的图像,就可以了,
  • 嗯,奇怪..我从 JSON 解析的图像是:cdn4.lbstatic.nu/files/looks/medium/2013/05/08/… 这应该有更大的高度..
  • 我认为你应该使用scrollView
  • 你是什么意思? @Muhannad
猜你喜欢
  • 1970-01-01
  • 2015-11-19
  • 1970-01-01
  • 2020-07-17
  • 1970-01-01
  • 1970-01-01
  • 2015-06-28
  • 1970-01-01
相关资源
最近更新 更多