Android如何实现九宫格

来源:爱站网时间:2020-05-14编辑:网友分享
今天我们在浏览网页的时候看到不少关于Android九宫格的内容,那么你知道Android如何实现九宫格吗?如果你还不了解的话就跟爱站小编一起去看看具体内容吧。

今天我们在浏览网页的时候看到不少关于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              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怎么设置应用全屏

下一篇:Android界面刷新的方法

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载