android 九宫格封装,Android 九宫格的实现方法
1、xml代碼:
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:background="@drawable/yellow"
>
android:layout_width="100sp"
android:layout_height="100sp"
android:layout_gravity="center_vertical"
android:background="@drawable/a">
android:id="@+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numcolumns="3"
android:verticalspacing="30dip"
android:horizontalspacing="10dip"
android:columnwidth="90dip"
android:stretchmode="columnwidth"
android:gravity="center"
android:listselector="@drawable/c"
>
其中android:numcolumns="3"? 代表九宮格的列數? auto_fit時為自動
2、實現代碼
public class mainactivity extends activity {
/** called when the activity is first created. */
@override
protected void oncreate(bundle savedinstancestate) {
// todo auto-generated method stub
super.oncreate(savedinstancestate);
// 設置屏幕沒有標題
this.requestwindowfeature(window.feature_no_title);
// 去掉標題欄
this.getwindow().setflags(windowmanager.layoutparams.flag_fullscreen,
windowmanager.layoutparams.flag_fullscreen);
setcontentview(r.layout.activity_main);
gridview gridview = (gridview) findviewbyid(r.id.gridview);
// 創建一個數組列表對象
arraylist> lstimageitem = new arraylist>();
/**
* 為每個格子添加內容
*/
for (int i = 1; i < 10; i++) {
hashmap map = new hashmap();// 建立hashmap對象
if (i == 1) {
map.put("itemimage", r.drawable.g11);
map.put("itemtext", getresources()
.getstring(r.string.gridview1));
}
if (i == 2) {
map.put("itemimage", r.drawable.g12);
map.put("itemtext", getresources()
.getstring(r.string.gridview2));
}
if (i == 3) {
map.put("itemimage", r.drawable.g13);
map.put("itemtext", getresources()
.getstring(r.string.gridview3));
}
if (i == 4) {
map.put("itemimage", r.drawable.g14);
map.put("itemtext", getresources()
.getstring(r.string.gridview4));
}
if (i == 5) {
map.put("itemimage", r.drawable.g15);
map.put("itemtext", getresources()
.getstring(r.string.gridview5));
}
if (i == 6) {
map.put("itemimage", r.drawable.g16);
map.put("itemtext", getresources()
.getstring(r.string.gridview6));
}
if (i == 7) {
map.put("itemimage", r.drawable.g17);
map.put("itemtext", getresources()
.getstring(r.string.gridview7));
}
if (i == 8) {
map.put("itemimage", r.drawable.g18);
map.put("itemtext", getresources()
.getstring(r.string.gridview8));
}
if (i == 9) {
map.put("itemimage", r.drawable.g19);
map.put("itemtext", getresources()
.getstring(r.string.gridview9));
}
lstimageitem.add(map);
}
/**
* 為gridview建立simpleadapter適配器
*/
// simpleadapter()中的五個參數分別是:第一個context,第二個數據資源,第三個每一個子項的布局文件,第四個每一個子項中的key數組
// 第五個每一個子項中的value數組
simpleadapter saimageitems = new simpleadapter(this, lstimageitem,
r.layout.grid_item, new string[] { "itemimage", "itemtext" },
new int[] { r.id.itemimage, r.id.itemtext });
gridview.setadapter(saimageitems);// 添加適配器
gridview.setonitemclicklistener(new itemclicklistener());// 為每一個子項設置監聽
}
class itemclicklistener implements onitemclicklistener {
@suppresswarnings("unchecked")
public void onitemclick(adapterview> arg0,// the adapterview where the
// click happened
view arg1,// the view within the adapterview that was clicked
int arg2,// the position of the view in the adapter
long arg3// the row id of the item that was clicked
) {
hashmap item = (hashmap) arg0
.getitematposition(arg2);
if (item.get("itemtext").equals(
getresources().getstring(r.string.gridview1))) {
toast.maketext(mainactivity.this, r.string.gridview1,
toast.length_long).show();
}
if (item.get("itemtext").equals(
getresources().getstring(r.string.gridview2))) {
toast.maketext(mainactivity.this, r.string.gridview2,
toast.length_long).show();
}
if (item.get("itemtext").equals(
getresources().getstring(r.string.gridview3))) {
toast.maketext(mainactivity.this, r.string.gridview3,
toast.length_long).show();
}
if (item.get("itemtext").equals(
getresources().getstring(r.string.gridview4))) {
toast.maketext(mainactivity.this, r.string.gridview4,
toast.length_long).show();
}
if (item.get("itemtext").equals(
getresources().getstring(r.string.gridview5))) {
toast.maketext(mainactivity.this, r.string.gridview5,
toast.length_long).show();
}
if (item.get("itemtext").equals(
getresources().getstring(r.string.gridview6))) {
toast.maketext(mainactivity.this, r.string.gridview6,
toast.length_long).show();
}
if (item.get("itemtext").equals(
getresources().getstring(r.string.gridview7))) {
toast.maketext(mainactivity.this, r.string.gridview7,
toast.length_long).show();
}
if (item.get("itemtext").equals(
getresources().getstring(r.string.gridview8))) {
toast.maketext(mainactivity.this, r.string.gridview8,
toast.length_long).show();
}
if (item.get("itemtext").equals(
getresources().getstring(r.string.gridview9))) {
toast.maketext(mainactivity.this, r.string.gridview9,
toast.length_long).show();
}
}
}
}
3、實現效果如圖所示
總結
以上是生活随笔為你收集整理的android 九宫格封装,Android 九宫格的实现方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android必须服务,说说在Andro
- 下一篇: android sina oauth2.