实现的效果:
项目布局:
具体代码如下:
1.MainActivity
package com.example.gridlayout_demo;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.GridView;
import android.widget.LinearLayout;
import com.example.gridlayout_demo.common.util.Tools;
import com.example.gridlayout_demo.model.BookInfo;
/**
* 网格布局的demo
*
* @作者: 刘倩</br>
* @时间: 2014年4月24日 上午11:35:06</br>
* @描述: 这个小demo主要是显示网格布局</br>
*/
public class MainActivity extends Activity
{
/** 显示的数据 */
private List<BookInfo> listData = new ArrayList<BookInfo>();
/** 网格布局 */
private GridLayoutAdapter gridLayoutAdapter;
/** 定义GridView */
private GridView gridView;
/** 线性布局 */
private LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// 设置无标题状态
requestWindowFeature(Window.FEATURE_NO_TITLE);
initView();
}
/**
* 初始化视图
*/
private void initView()
{
setContentView(R.layout.activity_main);
linearLayout = (LinearLayout) findViewById(R.id.activity_main_ll);
addListData();
getGridView();
}
/**
* 设置网格的布局
*/
private void getGridView()
{
if (gridView == null)
{
gridView = new GridView(MainActivity.this);
int screenWidth = Tools.getScreenWidth();
int screenHeight = Tools.getScreenHeight();
// 适配1024*600 屏幕(7寸手机)
if (screenHeight > 960 && screenHeight <= 1024 && screenWidth > 540
&& screenWidth <= 600)
{
gridView.setNumColumns(4);
} else
{
gridView.setNumColumns(3);
}
}
linearLayout.setVisibility(View.VISIBLE);
linearLayout.addView(gridView);
gridLayoutAdapter = new GridLayoutAdapter(MainActivity.this, listData);
gridView.setAdapter(gridLayoutAdapter);
}
/**
* 设置要显示的数据
*/
private void addListData()
{
int[] drawable =
{ R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher };
String[] bookName =
{ "总裁的小妻子", "总裁的小妻子", "总裁的小妻子", "总裁的小妻子", "总裁的小妻子", "总裁的小妻子", "总裁的小妻子",
"总裁的小妻子", "总裁的小妻子", "总裁的小妻子", "总裁的小妻子", "总裁的小妻子" };
for (int i = 0; i < 12; i++)
{
BookInfo bookInfo = new BookInfo();
bookInfo.setBookIcon(drawable[i]);
bookInfo.setBookName(bookName[i]);
listData.add(bookInfo);
}
}
}
2.GridLayoutAdapter
package com.example.gridlayout_demo;
import java.util.List;
import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.gridlayout_demo.common.util.Tools;
import com.example.gridlayout_demo.model.BookInfo;
/**
* 网格布局适配器
*
* @作者: 刘倩</br>
* @时间: 2014年4月24日 下午1:55:51</br>
* @描述: 主要实现网格的布局</br>
*/
public class GridLayoutAdapter extends BaseAdapter
{
/** 上下文 */
Activity activity;
/** 填充器 */
LayoutInflater layoutInflater;
/** 数据集合 */
List<BookInfo> listData;
public GridLayoutAdapter(Activity mainActivity, List<BookInfo> listData)
{
this.activity = mainActivity;
this.listData = listData;
layoutInflater = LayoutInflater.from(mainActivity);
}
@Override
public int getCount()
{
// 每行个数
int k = 0;
// 屏幕的宽度
int screenHeight = Tools.getScreenHeight();
// 屏幕的高度
int screenWidth = Tools.getScreenWidth();
if (screenHeight > 960 && screenHeight <= 1024 && screenWidth > 540
&& screenWidth <= 600)
{
k = 4;
} else
{
k = 3;
}
int virtualCount = listData.size();
if (virtualCount % k != 0)
{
virtualCount += (k - virtualCount % k);
}
return virtualCount;
}
@Override
public Object getItem(int position)
{
return listData.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder viewHolder;
if (convertView == null)
{
viewHolder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.listivew_grid_layout,
null);
viewHolder.ivBookIcon = (ImageView) convertView
.findViewById(R.id.listview_grid_layout_iv_bookicon);
viewHolder.tvBookName = (TextView) convertView
.findViewById(R.id.listview_grid_layout_tv_bookname);
convertView.setTag(viewHolder);
} else
{
viewHolder = (ViewHolder) convertView.getTag();
}
int screenWidth = Tools.getScreenWidth();
int screenHeight = Tools.getScreenHeight();
// 适配1024*600 屏幕(7寸手机)
if (screenHeight > 960 && screenHeight <= 1024 && screenWidth > 540
&& screenWidth <= 600)
{
if (position % 4 == 0)
{// 左边
((LinearLayout) convertView)
.setGravity(Gravity.CENTER_HORIZONTAL);
convertView.setPadding(0, Tools.dipToPixel(5), 0,
Tools.dipToPixel(10));
} else if (position % 4 == 1)
{// 中间
convertView.setPadding(0, Tools.dipToPixel(5), 0,
Tools.dipToPixel(10));
((LinearLayout) convertView)
.setGravity(Gravity.CENTER_HORIZONTAL);
} else if (position % 4 == 2)
{// 右边
convertView.setPadding(0, Tools.dipToPixel(5), 0,
Tools.dipToPixel(10));
((LinearLayout) convertView)
.setGravity(Gravity.CENTER_HORIZONTAL);
} else if (position % 4 == 3)
{
convertView.setPadding(0, Tools.dipToPixel(5), 0,
Tools.dipToPixel(10));
((LinearLayout) convertView)
.setGravity(Gravity.CENTER_HORIZONTAL);
}
} else
{
if (position % 3 == 0)
{// 左边
((LinearLayout) convertView).setGravity(Gravity.RIGHT);
convertView.setPadding(Tools.dipToPixel(3),
Tools.dipToPixel(5), 0, Tools.dipToPixel(10));
} else if (position % 3 == 1)
{// 中间
convertView.setPadding(0, Tools.dipToPixel(5), 0,
Tools.dipToPixel(10));
((LinearLayout) convertView)
.setGravity(Gravity.CENTER_HORIZONTAL);
} else if (position % 3 == 2)
{// 右边
convertView.setPadding(0, Tools.dipToPixel(5),
Tools.dipToPixel(3), Tools.dipToPixel(10));
((LinearLayout) convertView).setGravity(Gravity.LEFT);
}
}
/* 网格布局 */
BookInfo bookInfo = listData.get(position);
viewHolder.ivBookIcon.setBackgroundResource(bookInfo.getBookIcon());
viewHolder.tvBookName.setText(bookInfo.getBookName());
return convertView;
}
/**
* 存放控件的类
*
* @作者: 刘倩</br>
* @时间: 2014年4月24日 下午1:59:20</br>
* @描述: 这里主要存放书封的ImageView和书名的TextView</br>
*/
class ViewHolder
{
/** 书封 */
ImageView ivBookIcon;
/** 书名 */
TextView tvBookName;
}
}
3.Tools.java
package com.example.gridlayout_demo.common.util;
import com.example.gridlayout_demo.common.application.ApplicationData;
import android.util.DisplayMetrics;
/**
* 全局的工具类
*
* @作者: 刘倩</br>
* @时间: 2014年4月24日 下午2:16:20</br>
* @描述: 全局的工具类,主要是一些常用的方法</br>
*/
public class Tools
{
/**
* 获得屏幕的宽度
*
* @param context
* @return
*/
public static int getScreenWidth()
{
DisplayMetrics dm = new DisplayMetrics();
dm = ApplicationData.globalContext.getApplicationContext()
.getResources().getDisplayMetrics();
return dm.widthPixels;
}
/**
* 获得屏幕的高度
*
* @param context
* @return
*/
public static int getScreenHeight()
{
DisplayMetrics dm = new DisplayMetrics();
dm = ApplicationData.globalContext.getApplicationContext()
.getResources().getDisplayMetrics();
return dm.heightPixels;
}
/**
* dip转成pixel
*
* @param dip
* @return
*/
public static int dipToPixel(float dip)
{
return (int) (dip
* ApplicationData.globalContext.getResources()
.getDisplayMetrics().density + 0.5);
}
}
源码的下载地址:http://download.csdn.net/detail/u010820846/7245813