2015-04-20 112 views
-5

我正在設置一個ListView,它應該包含圖像縮略圖和文本。我想在屏幕上顯示5行,並根據設備將圖像縮放到可用空間。將Android圖像縮放到Listview項目

我做了以下佈局xml文件。第一個包含ListView,第二個包含列表項。我目前正在設置一個固定的大小。我如何改變佈局來縮放圖像以適應屏幕尺寸?

設置ListView控件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin"" > 

    <ListView 
    android:id="@+id/gallery" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
    </ListView> 

</FrameLayout> 

列表項佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="fill_parent" 
android:orientation="horizontal" > 
<ImageView 
android:id="@+id/icon" 
android:layout_width="50dp" 
android:layout_height="50dp" 
android:layout_marginBottom="5dp" 
android:layout_marginLeft="5dp" 
android:layout_marginRight="5dp" 
android:layout_marginTop="5dp" 
android:src="@raw/pic01" /> 

<TextView 
android:id="@+id/textview" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:textSize="20sp" 
android:paddingTop="5dp"/> 

</LinearLayout> 

回答

0

我覺得這行代碼將有助於you.In您的適配器類,在getView功能把這些線代碼

switch (ctx.getResources().getDisplayMetrics().densityDpi) { 
       case DisplayMetrics.DENSITY_LOW: 
        setImageSize(appIcon, 36, 36); 
        break; 
       case DisplayMetrics.DENSITY_MEDIUM: 
        setImageSize(appIcon, 48, 48); 
        break; 
       case DisplayMetrics.DENSITY_HIGH: 
        setImageSize(appIcon, 72, 72); 
        break; 
       case DisplayMetrics.DENSITY_XHIGH: 
        setImageSize(appIcon, 96, 96); 
        break; 
       case DisplayMetrics.DENSITY_400: 
        setImageSize(appIcon, 96, 96); 
        break; 
       case DisplayMetrics.DENSITY_XXHIGH: 
        setImageSize(appIcon, 144, 144); 
        break; 
       case DisplayMetrics.DENSITY_560: 
        setImageSize(appIcon, 144, 144); 
        break; 
       case DisplayMetrics.DENSITY_XXXHIGH: 
        setImageSize(appIcon, 192, 192); 
        break; 
       default: 
        setImageSize(appIcon, 48, 48); 
       }    

這裏是setImageSize功能

 private void setImageSize(ImageView appIcon, int width, int height) { 
    try { 
     appIcon.getLayoutParams().width = width; 
     appIcon.getLayoutParams().height = height; 
    } catch (Exception ex) { 
     DebugLog.console(ex, "[UserInfoAdapter]: Exception in setImageSize");   
     EmailDebugLog.getInstance(ctx).writeLog(ex.toString()+ "\r\n[UserInfoAdapter]: Exception occured inside setImageSize"); 
    } 
}