Android实现为GridView添加边框效果

为GridView添加边框效果,供大家参考,具体内容如下

网站建设哪家好,找成都创新互联!专注于网页设计、网站建设、微信开发、微信小程序、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了邛崃免费建站欢迎大家使用!

1.自定义GridView的item样式:

grid_item.xml文件

<?xml version="1.0" encoding="utf-8"?> 
 
   
 

2.设置GridView item的背景:
grid_selector.xml文件

<?xml version="1.0" encoding="utf-8"?> 
   
    
      
   
 

3.设置GridView的listSelector为透明:

android:listSelector="@android:color/transparent" 

4.自定义GridView的adapter:

import java.util.ArrayList; 
import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
 
public class GridAdapter extends BaseAdapter { 
  private Context mContext = null; 
  private LayoutInflater inflater = null; 
  private ArrayList drawableList = null; 
 
  public GridAdapter(Context c) { 
    this.mContext = c; 
    this.inflater = LayoutInflater.from(mContext); 
  } 
  public void setDataSource(ArrayList list){ 
    this.drawableList = list; 
  } 
  public int getCount() { 
    if(drawableList != null){ 
      return drawableList.size(); 
    }else{ 
      return 0; 
    } 
  } 
 
  public Object getItem(int position) { 
    return drawableList.get(position); 
  } 
 
  public long getItemId(int position) { 
    return position; 
  } 
 
  public View getView(int position, View convertView, ViewGroup parent) { 
 
    ImageView imageView = new ImageView(mContext); 
    if (convertView == null) { 
      convertView = inflater.inflate(R.layout.grid_item, null); 
      imageView = (ImageView) convertView.findViewById(R.id.item_image); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
      convertView.setTag(imageView); 
    } else { 
      imageView = (ImageView) convertView.getTag(); 
    } 
    imageView.setImageDrawable(drawableList.get(position)); 
    return convertView; 
  } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


当前题目:Android实现为GridView添加边框效果
分享地址:http://pwwzsj.com/article/jppdde.html