2013-05-09 34 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

相關問題