Android GridView 一行显示数据(包括图片和文本),解决的办法是计算数据占该行的宽度是多少

最近在做图片的浏览功能,开始是使用Gallery做,但是,达不到我想要的效果,关于使用Gallery显示缩略图的缺点和优点,不在详述了。以下是一个完整的Demo代码,注意我的模拟器是640*960。

  1. package com.treasure.ui;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.GridView;  
  6. import android.widget.LinearLayout;  
  7. import android.widget.LinearLayout.LayoutParams;  
  8.   
  9. import com.treasure.adapter.Album1Adapter;  
  10. import com.treasure.utils.DisplayUtil;  
  11.   
  12. public class TestGallery extends Activity  
  13. {  
  14.     private Album1Adapter albumAdapter;  
  15.     private GridView albumGallery;  
  16.       
  17.     private int[] resIds = new int[]  
  18.             {R.drawable.a, R.drawable.b, R.drawable.c,  
  19.             R.drawable.d, R.drawable.e, R.drawable.f,  
  20.             R.drawable.g, R.drawable.h, R.drawable.i,  
  21.             R.drawable.j, R.drawable.k, R.drawable.l,  
  22.             R.drawable.m};  
  23.       
  24.     private String[] titles = new String[]  
  25.             {"a""b""c""d""e""f""g""h""i",  
  26.             "j""k""l""m"};  
  27.       
  28.     @Override  
  29.     public void onCreate(Bundle savedInstanceState)  
  30.     {  
  31.         super.onCreate(savedInstanceState);  
  32.         setContentView(R.layout.display_image);  
  33.           
  34.         albumAdapter = new Album1Adapter(this, resIds, titles);  
  35.         albumGallery = (GridView)findViewById(R.id.album_gallery);  
  36.           
  37.         albumGallery.setAdapter(albumAdapter);  
  38.         albumGallery.setNumColumns(resIds.length);  
  39.           
  40.         // 计算一行显示图片需要的宽度  
  41.         LinearLayout layout = (LinearLayout)findViewById(R.id.linear_id);  
  42.         android.view.ViewGroup.LayoutParams params = layout.getLayoutParams();  
  43.           
  44.         // 注意:根据自己程序使用什么为单位进行设置,我使用的是dp,所以需要将dp转换成px,这样才能得到实现的宽度  
  45.         // 如果你不转换单位的话,它默认的是px的  
  46.         // 95 * (resIds.length + 1)  
  47.         // 其中95表示的是android:columnWidth="95dp"  
  48.         // (resIds.length + 1)表示的是获得相册的个数加1  
  49.         params.width = DisplayUtil.dip2px(this95 * (resIds.length + 1));  
  50.         params.height = LayoutParams.WRAP_CONTENT;  
  51.         layout.setLayoutParams(params);  
  52.         albumGallery.setSelection(0);  
  53.     }  
  54. }  
  1. package com.treasure.utils;  
  2.   
  3. import android.content.Context;  
  4.   
  5. /** 
  6.  * 转换手机分辨率的类 
  7.  * @author Treasure 
  8.  * 
  9.  */  
  10. public class DisplayUtil   
  11. {  
  12.     /** 
  13.      * 根据手机的分辨率从 dp 的单位 转成为 px(像素) 
  14.      */  
  15.     public static int dip2px(Context context, float dpValue)  
  16.     {  
  17.         final float scale = context.getResources().getDisplayMetrics().density;  
  18.         return (int) (dpValue * scale + 0.5f);  
  19.     }  
  20.        
  21.     /** 
  22.      * 根据手机的分辨率从 px(像素) 的单位 转成为 dp 
  23.      */  
  24.     public static int px2dip(Context context, float pxValue)  
  25.     {  
  26.         final float scale = context.getResources().getDisplayMetrics().density;  
  27.         return (int) (pxValue / scale + 0.5f);  
  28.     }  
  29. }  
  1. package com.treasure.adapter;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import com.treasure.ui.R;  
  6.   
  7. import android.content.Context;  
  8. import android.view.LayoutInflater;  
  9. import android.view.View;  
  10. import android.view.ViewGroup;  
  11. import android.widget.BaseAdapter;  
  12. import android.widget.ImageView;  
  13. import android.widget.TextView;  
  14.   
  15. /** 
  16.  * 自定义相册的适配器类 
  17.  * @author Treasure 
  18.  * 
  19.  */  
  20. public class Album1Adapter extends BaseAdapter  
  21. {  
  22.   
  23.     private int[] images;  
  24.     private String[] titles;  
  25.     private ArrayList<GalleryInfo> list;  
  26.     private LayoutInflater inflater;  
  27.       
  28.     public Album1Adapter(Context context, int[] images, String[] titles)  
  29.     {  
  30.         this.images = images;  
  31.         this.titles = titles;  
  32.         list = new ArrayList<GalleryInfo>();  
  33.         inflater = LayoutInflater.from(context);  
  34.           
  35.         for (int i = 0; i < images.length; i++)  
  36.         {  
  37.             GalleryInfo info = new GalleryInfo();  
  38.             info.title = this.titles[i];  
  39.             info.drawable = this.images[i];  
  40.             if (i == 0)  
  41.             {  
  42.                 info.isSelect = true;  
  43.             }  
  44.             else  
  45.             {  
  46.                 info.isSelect = false;  
  47.             }  
  48.             list.add(info);  
  49.         }  
  50.     }  
  51.       
  52.     @Override  
  53.     public int getCount()  
  54.     {  
  55.         return titles.length;  
  56.     }  
  57.   
  58.     @Override  
  59.     public Object getItem(int position)  
  60.     {  
  61.         return position;  
  62.     }  
  63.   
  64.     @Override  
  65.     public long getItemId(int position)  
  66.     {  
  67.         return position;  
  68.     }  
  69.   
  70.     @Override  
  71.     public View getView(int position, View convertView, ViewGroup parent)  
  72.     {  
  73.         ViewHolder holder;  
  74.         if (convertView == null)  
  75.         {  
  76.             convertView = inflater.inflate(R.layout.album_gallery_item, null);  
  77.             holder = new ViewHolder();  
  78.             holder.photoFrameImg =   
  79.                     (ImageView)convertView.findViewById(R.id.photo_frame_column_02);  
  80.             holder.imageImg =   
  81.                     (ImageView)convertView.findViewById(R.id.image_column_02);  
  82.             holder.titleTxt =   
  83.                     (TextView)convertView.findViewById(R.id.photo_name_column_02);  
  84.             convertView.setTag(holder);  
  85.         }  
  86.         else  
  87.         {  
  88.             holder = (ViewHolder)convertView.getTag();  
  89.         }  
  90.           
  91.         holder.photoFrameImg.setImageResource(R.drawable.photo_frame);  
  92.         holder.imageImg.setImageResource(list.get(position).drawable);  
  93.         holder.titleTxt.setText(list.get(position).title);  
  94.           
  95.         if (list.get(position).isSelect)  
  96.         {  
  97.             holder.imageImg.setBackgroundResource(R.drawable.gallery_select);  
  98.         }  
  99.         else  
  100.         {  
  101.             holder.imageImg.setBackgroundDrawable(null);  
  102.         }  
  103.           
  104.         return convertView;  
  105.     }  
  106.       
  107.     public void changeStatus(int select)  
  108.     {  
  109.         for (int i = 0; i < list.size(); i++)  
  110.         {  
  111.             list.get(i).isSelect = false;  
  112.         }  
  113.         list.get(select).isSelect = true;  
  114.     }  
  115.       
  116.     private class ViewHolder  
  117.     {  
  118.         ImageView photoFrameImg;  
  119.         ImageView imageImg;  
  120.         TextView titleTxt;  
  121.     }  
  122.   
  123.     private class GalleryInfo  
  124.     {  
  125.         public String title;  
  126.         public int drawable;  
  127.         private boolean isSelect;  
  128.     }  
  129. }  


res/layout/album_gallery_item.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content">  
  5.       
  6.     <ImageView  
  7.         android:id="@+id/photo_frame_column_02"  
  8.         android:layout_width="86dp"  
  9.         android:layout_height="90dp" />  
  10.   
  11.     <ImageView  
  12.         android:id="@+id/image_column_02"  
  13.         android:layout_width="65dp"  
  14.         android:layout_height="60dp"  
  15.         android:layout_marginLeft="11dp"  
  16.         android:layout_marginTop="14dp"   
  17.         android:scaleType="fitXY"/>  
  18.   
  19.     <TextView  
  20.         android:id="@+id/photo_name_column_02"  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"  
  23.         android:layout_below="@id/photo_frame_column_02"  
  24.         android:layout_centerHorizontal="true"  
  25.         android:paddingTop="2dp"  
  26.         android:textColor="@color/white" />  
  27.   
  28. </RelativeLayout>  


res/layout/display_image.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical"  
  6.     android:background="@color/white" >  
  7.       
  8.     <RelativeLayout android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content">  
  10.         <LinearLayout android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:orientation="vertical"  
  13.         android:layout_marginTop="84dp"  
  14.         android:layout_marginLeft="3dp">  
  15.         <ImageView android:layout_width="wrap_content"  
  16.             android:layout_height="wrap_content"  
  17.             android:src="@drawable/table_top" />  
  18.         <ImageView android:layout_width="wrap_content"  
  19.             android:layout_height="wrap_content"  
  20.             android:src="@drawable/table_under" />  
  21.     </LinearLayout>  
  22.     <HorizontalScrollView   
  23.         android:id="@+id/galleryScroll"  
  24.         android:layout_width="fill_parent"  
  25.         android:layout_height="wrap_content"  
  26.         android:scrollbars="none"  
  27.         android:focusable="false"  
  28.         >  
  29.         <FrameLayout   
  30.             android:layout_width="fill_parent"  
  31.             android:layout_height="wrap_content"  
  32.             android:focusable="false"  
  33.             >  
  34.             <LinearLayout   
  35.                 android:id="@+id/linear_id"  
  36.                 android:layout_width="1330dp"  
  37.                 android:layout_height="wrap_content"   
  38.                 android:orientation="horizontal"  
  39.                 android:focusable="false"  
  40.                 android:paddingLeft="12dp"  
  41.                 >  
  42.                 <GridView android:id="@+id/album_gallery"   
  43.                     android:layout_width="fill_parent"  
  44.                     android:gravity="center"   
  45.                     android:layout_height="wrap_content"  
  46.                     android:horizontalSpacing="1.0dp"   
  47.                     android:verticalSpacing="1.0dp"  
  48.                     android:stretchMode="spacingWidthUniform"   
  49.                     android:numColumns="auto_fit"  
  50.                     android:columnWidth="95dp"  
  51.                     android:focusable="false"  
  52.                     >  
  53.                 </GridView>  
  54.   
  55.             </LinearLayout>  
  56.         </FrameLayout>  
  57.     </HorizontalScrollView>  
  58.     </RelativeLayout>  
  59.   
  60. </LinearLayout>  


效果图:

未滚动,如图所示:


滚动到中间,如图所示:


滚动到结尾,如图所示:

原文地址:https://www.cnblogs.com/Free-Thinker/p/3685456.html