日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android UI开发第一篇——android的九宫格式实现

發布時間:2025/5/22 Android 123 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android UI开发第一篇——android的九宫格式实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

今天在devdiv論壇里看到有壇友問到九宮格的實現,我把我在項目中用的經驗分享一下。


代碼地址:http://download.csdn.net/detail/xyz_lmn/5151879


xml代碼: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res/com.google.android.gx5weather" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0" android:background="@drawable/bg" > <ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:background="@drawable/top"></ImageView>?? <GridView xmlns:android="http://schemas.android.com/apk/res/android"??? ??? 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/grid_selector_background" > </GridView> </LinearLayout>
android:numColumns="3" //九宮格的列數? auto_fit時為自動 android:listSelector="@drawable/grid_selector_background"?? //九宮格的背景,可以找個圓角正方形

public class NineBox 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.main_activity);

??????? GridView gridview=(GridView)findViewById(R.id.gridview);

??????? ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();??

??????? for(int i=1;i<10;i++)??

??????? {??

????????? HashMap<String, Object> map = new HashMap<String, Object>();

????????? 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);

?????????

??????? }??


?

??????? 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<String, Object> item=(HashMap<String, Object>) arg0.getItemAtPosition(arg2);??

???

????? if(item.get("ItemText").equals(getResources().getString(R.string.gridview1))){

??? ? Toast.makeText(NineBox.this, R.string.gridview1, Toast.LENGTH_LONG).show();

????? }

????? if(item.get("ItemText").equals(getResources().getString(R.string.gridview2))){

??? ? Toast.makeText(NineBox.this, R.string.gridview2, Toast.LENGTH_LONG).show();

????? }

????? if(item.get("ItemText").equals(getResources().getString(R.string.gridview3))){

??? ? Toast.makeText(NineBox.this, R.string.gridview3, Toast.LENGTH_LONG).show();

????? }

????? if(item.get("ItemText").equals(getResources().getString(R.string.gridview4))){

??? ? Toast.makeText(NineBox.this, R.string.gridview4, Toast.LENGTH_LONG).show();

????? }

????? if(item.get("ItemText").equals(getResources().getString(R.string.gridview5))){

??? ? Toast.makeText(NineBox.this, R.string.gridview5, Toast.LENGTH_LONG).show();

????? }

????? if(item.get("ItemText").equals(getResources().getString(R.string.gridview6))){

??? ? Toast.makeText(NineBox.this, R.string.gridview6, Toast.LENGTH_LONG).show();

????? }


?

????? if(item.get("ItemText").equals(getResources().getString(R.string.gridview7))){

??? ? Toast.makeText(NineBox.this, R.string.gridview7, Toast.LENGTH_LONG).show();

????? }

????? if(item.get("ItemText").equals(getResources().getString(R.string.gridview8))){

??? ? Toast.makeText(NineBox.this, R.string.gridview8, Toast.LENGTH_LONG).show();

????? }

????? if(item.get("ItemText").equals(getResources().getString(R.string.gridview9))){

??? ? Toast.makeText(NineBox.this, R.string.gridview9, Toast.LENGTH_LONG).show();

????? }

???? }??

??? }

}

?

/**
* @author xyz_lmn
* 郵箱:xy-zhang@163.com
*
*/

轉載于:https://my.oschina.net/201003674/blog/288951

總結

以上是生活随笔為你收集整理的Android UI开发第一篇——android的九宫格式实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。