不一樣分辨率下網格佈局的實現

實現的效果:
java

項目佈局:android

具體代碼以下:app

1.MainActivityide

package com.example.gridlayout_demo;工具


import java.util.ArrayList;佈局

import java.util.List;this


import android.app.Activity;spa

import android.os.Bundle;.net

import android.view.View;ip

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

相關文章
相關標籤/搜索