2013-05-09 71 views
2

我有一个JSON,我解析JSON。它带有文本和列表视图中的图像。 除了一件事以外,一切正常。 我想获得ImageView fill_parent作为宽度和wrap_content作为高度。这不工作..Android - 从ImageView URL不会fill_parent ListView

这是我的布局:

<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" /> 

如果你需要,这是我的Java显示在列表视图图像: http://pastebin.com/N0j5gjPE

谁能帮我?

+0

你需要的图片,作为其母公司的全宽? – 2013-05-09 13:05:14

+1

@Muhannad是,图像是不同的高度,因此宽度必须填补其母公司的宽度和高度取决于原始比例 – GromDroid 2013-05-09 13:09:19

+1

尝试给scaleType的图像视图fitXY – Triode 2013-05-09 13:10:12

回答

4

试试这个:

 <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"/> 

从DOC:

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"/> 
+0

这是一半的工作。这个比例是好的,但它不能填满整个宽度。我改变了android:widht =“fill_parent”但这没有任何帮助.. – GromDroid 2013-05-09 13:52:54

+0

更新了回答:) – Chronos 2013-05-09 14:30:36

+0

谢谢! :D这工作! – GromDroid 2013-05-09 14:48:27

0

你可以试试这个属性添加到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" /> 
+0

它已经取得进展:D现在的宽度工作,除了高度..在这里,看看:http://i.imgur.com/XswW73R.jpg – GromDroid 2013-05-09 13:17:09

+0

这是因为你设置wrap_content,所以它会按原样包裹图像高度,所以尝试使用另一个图像的高度伟大的比现有的,它会很好, – 2013-05-09 13:19:25

+0

嗯,奇怪..我从JSON解析的图像是:http://cdn4.lbstatic.nu/files/looks/medium/2013/05/08/3024849_6889.jpg ?1368016292这应该有更大的高度.. – GromDroid 2013-05-09 13:24:50