实现九宫格
Main.java
package
cn.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class Main extends Activity {
private Button btn_viewItemAndText;
private Button btn_viewGridViewList;
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);
initControl();
setControlListener();
}
/**
* 初始化控件
*/
private void initControl(){
btn_viewItemAndText = (Button)findViewById(R.id.btn_viewItemAndText);
btn_viewGridViewList = (Button)findViewById(R.id.btn_viewGridViewList);
}
/**
* 增加控件监听
*/
private void setControlListener(){
btn_viewItemAndText.setOnClickListener(onClickViewItemAndTextListener);
btn_viewGridViewList.setOnClickListener(onCLickViewGridViewListListener);
}
/**
* 图文列表按钮事件
*/
private View.OnClickListener onClickViewItemAndTextListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v( " OnClick " , " btn_viewItemAndText " );
Intent intent = new Intent(Main. this , ItemAndTextListView. class );
startActivity(intent);
}
};
private View.OnClickListener onCLickViewGridViewListListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v( " OnClick " , " btn_viewGridViewList " );
Intent intent = new Intent(Main. this , GridViewList. class );
startActivity(intent);
}
};
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class Main extends Activity {
private Button btn_viewItemAndText;
private Button btn_viewGridViewList;
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);
initControl();
setControlListener();
}
/**
* 初始化控件
*/
private void initControl(){
btn_viewItemAndText = (Button)findViewById(R.id.btn_viewItemAndText);
btn_viewGridViewList = (Button)findViewById(R.id.btn_viewGridViewList);
}
/**
* 增加控件监听
*/
private void setControlListener(){
btn_viewItemAndText.setOnClickListener(onClickViewItemAndTextListener);
btn_viewGridViewList.setOnClickListener(onCLickViewGridViewListListener);
}
/**
* 图文列表按钮事件
*/
private View.OnClickListener onClickViewItemAndTextListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v( " OnClick " , " btn_viewItemAndText " );
Intent intent = new Intent(Main. this , ItemAndTextListView. class );
startActivity(intent);
}
};
private View.OnClickListener onCLickViewGridViewListListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v( " OnClick " , " btn_viewGridViewList " );
Intent intent = new Intent(Main. this , GridViewList. class );
startActivity(intent);
}
};
}
GridViewList.java
package
cn.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.SimpleAdapter;
public class GridViewList extends Activity {
private GridView gridview;
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.gridview);
initControl();
}
/**
* 初始加载控件
*/
private void initControl(){
SimpleAdapter adapter = new SimpleAdapter( this , getData(), R.layout.gridviewitem, new String[]{ " ItemImage " , " ItemText " }, new int []{R.id.ItemImage, R.id.ItemText});
gridview = (GridView)findViewById(R.id.gridView1);
gridview.setAdapter(adapter);
}
/**
* 模拟数据
* @return
*/
private List < Map < String, Object >> getData() {
List < Map < String, Object >> items = new ArrayList < Map < String, Object >> ();
for ( int i = 0 ; i < 20 ; i ++ ) {
Map < String, Object > item = new HashMap < String, Object > ();
item.put( " ItemImage " , R.drawable.icon);
item.put( " ItemText " , " 九宫项目 " + i);
items.add(item);
}
return items;
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.SimpleAdapter;
public class GridViewList extends Activity {
private GridView gridview;
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.gridview);
initControl();
}
/**
* 初始加载控件
*/
private void initControl(){
SimpleAdapter adapter = new SimpleAdapter( this , getData(), R.layout.gridviewitem, new String[]{ " ItemImage " , " ItemText " }, new int []{R.id.ItemImage, R.id.ItemText});
gridview = (GridView)findViewById(R.id.gridView1);
gridview.setAdapter(adapter);
}
/**
* 模拟数据
* @return
*/
private List < Map < String, Object >> getData() {
List < Map < String, Object >> items = new ArrayList < Map < String, Object >> ();
for ( int i = 0 ; i < 20 ; i ++ ) {
Map < String, Object > item = new HashMap < String, Object > ();
item.put( " ItemImage " , R.drawable.icon);
item.put( " ItemText " , " 九宫项目 " + i);
items.add(item);
}
return items;
}
}
gridview.xml
<?
xml version="1.0" encoding="utf-8"
?>
< LinearLayout
xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation ="vertical"
android:layout_width ="match_parent"
android:layout_height ="match_parent" >
< GridView android:layout_height ="wrap_content" android:id ="@+id/gridView1"
android:layout_width ="match_parent" android:numColumns ="auto_fit"
android:horizontalSpacing ="10dp" android:verticalSpacing ="10dp"
android:columnWidth ="90dp" android:stretchMode ="columnWidth"
android:gravity ="center" >
</ GridView >
</ LinearLayout >
< LinearLayout
xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation ="vertical"
android:layout_width ="match_parent"
android:layout_height ="match_parent" >
< GridView android:layout_height ="wrap_content" android:id ="@+id/gridView1"
android:layout_width ="match_parent" android:numColumns ="auto_fit"
android:horizontalSpacing ="10dp" android:verticalSpacing ="10dp"
android:columnWidth ="90dp" android:stretchMode ="columnWidth"
android:gravity ="center" >
</ GridView >
</ LinearLayout >
gridviewitem.xml
<?
xml version="1.0" encoding="utf-8"
?>
< RelativeLayout
xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation ="vertical"
android:layout_width ="match_parent"
android:layout_height ="match_parent" >
< ImageView android:layout_width ="wrap_content"
android:layout_height ="wrap_content" android:id ="@+id/ItemImage" ></ ImageView >
< TextView android:layout_height ="wrap_content" android:id ="@+id/ItemText"
android:layout_width ="wrap_content" android:layout_below ="@+id/ItemImage" ></ TextView >
</ RelativeLayout >
< RelativeLayout
xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation ="vertical"
android:layout_width ="match_parent"
android:layout_height ="match_parent" >
< ImageView android:layout_width ="wrap_content"
android:layout_height ="wrap_content" android:id ="@+id/ItemImage" ></ ImageView >
< TextView android:layout_height ="wrap_content" android:id ="@+id/ItemText"
android:layout_width ="wrap_content" android:layout_below ="@+id/ItemImage" ></ TextView >
</ RelativeLayout >
AndroidManifest.xml
<?
xml version="1.0" encoding="utf-8"
?>
< manifest xmlns:android ="http://schemas.android.com/apk/res/android"
package ="cn.test"
android:versionCode ="1"
android:versionName ="1.0" >
< uses-sdk android:minSdkVersion ="8" />
< application android:icon ="@drawable/icon" android:label ="@string/app_name" >
< activity android:name =".Main"
android:label ="@string/app_name" >
< intent-filter >
< action android:name ="android.intent.action.MAIN" />
< category android:name ="android.intent.category.LAUNCHER" />
</ intent-filter >
</ activity >
< activity android:name =".ItemAndTextListView"
android:label ="@string/app_name" >
< intent-filter >
< action android:name ="android.intent.action.itemAndTextListView" />
< category android:name ="android.intent.category.DEFAULT" />
</ intent-filter >
</ activity >
< activity android:name =".GridViewList"
android:label ="@string/app_name" >
< intent-filter >
< action android:name ="android.intent.action.gridViewList" />
< category android:name ="android.intent.category.DEFAULT" />
</ intent-filter >
</ activity >
</ application >
</ manifest >
< manifest xmlns:android ="http://schemas.android.com/apk/res/android"
package ="cn.test"
android:versionCode ="1"
android:versionName ="1.0" >
< uses-sdk android:minSdkVersion ="8" />
< application android:icon ="@drawable/icon" android:label ="@string/app_name" >
< activity android:name =".Main"
android:label ="@string/app_name" >
< intent-filter >
< action android:name ="android.intent.action.MAIN" />
< category android:name ="android.intent.category.LAUNCHER" />
</ intent-filter >
</ activity >
< activity android:name =".ItemAndTextListView"
android:label ="@string/app_name" >
< intent-filter >
< action android:name ="android.intent.action.itemAndTextListView" />
< category android:name ="android.intent.category.DEFAULT" />
</ intent-filter >
</ activity >
< activity android:name =".GridViewList"
android:label ="@string/app_name" >
< intent-filter >
< action android:name ="android.intent.action.gridViewList" />
< category android:name ="android.intent.category.DEFAULT" />
</ intent-filter >
</ activity >
</ application >
</ manifest >