亲宝软件园·资讯

展开

GridView添加边框效果 Android实现为GridView添加边框效果

datao819 人气:0
想了解Android实现为GridView添加边框效果的相关内容吗,datao819在本文为您仔细讲解GridView添加边框效果的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:GridView,边框,下面大家一起来学习吧。

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

1.自定义GridView的item样式:

grid_item.xml文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" > 
  <ImageView 
    android:id="@+id/item_image" 
    android:layout_width="115dp" 
    android:layout_height="115dp" 
    android:padding="8dp" 
    android:background="@drawable/grid_selector"  
    /> 
</LinearLayout> 

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

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">   
  <item android:state_window_focused="false"  
    android:drawable="@drawable/grid_normal" />  
  <item android:state_selected="true" 
    android:drawable="@drawable/grid_pressed" />    
  <item android:state_pressed="true" 
    android:drawable="@drawable/grid_pressed" /> 
</selector> 

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<Drawable> drawableList = null; 
 
  public GridAdapter(Context c) { 
    this.mContext = c; 
    this.inflater = LayoutInflater.from(mContext); 
  } 
  public void setDataSource(ArrayList<Drawable> 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; 
  } 
} 

加载全部内容

相关教程
猜你喜欢
用户评论