android九宫格实现

android九宫格实现,开始以为很复杂,其实只要知道了如何布局就很简单了。

先分析一下原理:

1:我们需要先定义一个主布局文件,宽度和列数用自适应的

android:numColumns="auto_fit"

android:stretchMode="columnWidth"

2:在定义一个子布局,子布局主要是控制九宫格每格的布局,例子是图文垂直布局。

3:在代码里面用SimpleAdapter将数据复制给布局文件,循环布局,就实现了九宫格

直接上代码:android九宫格主布局文件main.xml

Java代码

<?xmlversion="1.0"encoding="utf-8"?>

<GridViewxmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/gridView1"

android:layout_width="fill_parent"

android:numColumns="auto_fit"

android:stretchMode="columnWidth"

android:columnwidth="75dp"

android:gravity="center"

android:layout_height="fill_parent">

</GridView>

<?xmlversion="1.0"encoding="utf-8"?>

<GridViewxmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/gridView1"

android:layout_width="fill_parent"

android:numColumns="auto_fit"

android:stretchMode="columnWidth"

android:columnwidth="75dp"

android:gravity="center"

android:layout_height="fill_parent">

</GridView>

android九宫格子布局文件layout_gridview_item.xml

Java代码

<?xmlversion="1.0"encoding="utf-8"?>

<RelativeLayoutandroid:id="@+id/relativeLayout1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

xmlns:android="http://schemas.android.com/apk/res/android">

<ImageViewandroid:id="@+id/imageView_ItemImage"

android:layout_width="wrap_content"

android:src="@drawable/icon"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

>

</ImageView>

<TextViewandroid:id="@+id/textView_ItemText"

android:text="TextView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/imageView_ItemImage"

android:layout_centerHorizontal="true"

>

</TextView>

</RelativeLayout>

<?xmlversion="1.0"encoding="utf-8"?>

<RelativeLayoutandroid:id="@+id/relativeLayout1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

xmlns:android="http://schemas.android.com/apk/res/android">

<ImageViewandroid:id="@+id/imageView_ItemImage"

android:layout_width="wrap_content"

android:src="@drawable/icon"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

>

</ImageView>

<TextViewandroid:id="@+id/textView_ItemText"

android:text="TextView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/imageView_ItemImage"

android:layout_centerHorizontal="true"

>

</TextView>

</RelativeLayout>

android九宫格代码文件:

Java代码

packagecom.mixian.activity;

importjava.util.ArrayList;

importjava.util.HashMap;

importjava.util.List;

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.widget.AdapterView;

importandroid.widget.AdapterView.OnItemClickListener;

importandroid.widget.GridView;

importandroid.widget.SimpleAdapter;

importandroid.widget.Toast;

publicclassGridLayoutActivityextendsActivity{

/**Calledwhentheactivityisfirstcreated.*/

privateGridView_gridView1;

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

_gridView1=(GridView)findViewById(R.id.gridView1);

List<HashMap<String,Object>>lst=newArrayList<HashMap<String,Object>>();

for(inti=0;i<12;i++){

HashMap<String,Object>map=newHashMap<String,Object>();

map.put("itemImage",R.drawable.icon);

map.put("itemText","item"+i);

lst.add(map);

}

SimpleAdapteradpter=newSimpleAdapter(this,lst,

R.layout.layout_gridview_item,//指定子项的布局文件的ID

newString[]{"itemImage","itemText"},//指定数据的列

newint[]{R.id.imageView_ItemImage,R.id.textView_ItemText});//指定数据的列对应到的

//绑定的目标控件

_gridView1.setAdapter(adpter);

_gridView1.setOnItemClickListener(onclickcistener);

}

privateOnItemClickListeneronclickcistener=newOnItemClickListener(){

publicvoidonItemClick(AdapterView<?>arg0,Viewarg1,intarg2,

longarg3){

Objectobj=_gridView1.getAdapter().getItem(arg2);

HashMap<String,Object>map=(HashMap<String,Object>)obj;

Stringstr=(String)map.get("itemText");

Toast.makeText(getApplicationContext(),""+str,0).show();

}

};

}

相关推荐