Android之UI学习篇8:使用GridView实现九宫格的菜单

Android之UI学习篇八:使用GridView实现九宫格的菜单

今天在devdiv论坛里看到有坛友问到九宫格的实现,我把我在项目中用的经验分享一下,九宫格用gridview实现代码。

九宫格菜单通常是全屏显示的,那么如何控制某个Activity全屏显示呢,有两种方法:

方法一:

       在该Activity的onCreate函数中添加控制代码:

               this.requestWindowFeature(Window.FEATURE_NO_TITLE);  //设置Activity标题不显示
               this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);  //设置全屏显示

       注意:这两句代码必须写在setContentView函数的前面,否则运行会报错。

 

方法二:

        使用XML配置文件进行配置:

        (1) res/values/目录下新增title.xml文件,用来定义onTitle的样式,代码如下:

              <?xml version="1.0" encoding="utf-8"?>
              <resources>
                     <style name="noTitle">
                                 <item name="android:windowNoTitle">true</item> 
                     </style>
             </resources>

        (2) 在AndroidManifest.xml文件中,对Activity添加代码:android:theme="@style/noTitle"

     

接下来开始开发九宫格的菜单

开发出来的界面截图:

      Android之UI学习篇8:使用GridView实现九宫格的菜单

 

   开发步骤:

   (一) 放入图标资源到res/drawble-mdpi/目录下;

   (二) 在layout下新增item.xml(对于单个格子的布局),代码如下:

           <?xml version="1.0" encoding="utf-8"?>
           <LinearLayout  
                    xmlns:android="http://schemas.android.com/apk/res/android"  
                    android:orientation="vertical"  
                     android:layout_width="fill_parent"  
                      android:layout_height="wrap_content">  
                             <ImageView   
                                        android:id="@+id/ItemImageView"  
                                         android:layout_width="fill_parent"  
                                         android:layout_height="wrap_content"  
                                         android:layout_gravity="center"/>  
                             <TextView  
                                         android:id="@+id/ItemTextView"   
                                         android:layout_width="fill_parent"  
                                         android:layout_height="wrap_content"  
                                         android:gravity="center"/>  
              </LinearLayout>

   (三) 编写Layout/main.xml文件,代码如下:

           <?xml version="1.0" encoding="utf-8"?>
           <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical" >
                    <GridView xmlns:android="http://schemas.android.com/apk/res/android" 
                               android:id="@+id/MyGridView"   
                               android:layout_alignParentTop="true"  
                                android:layout_width="fill_parent"   
                                android:layout_height="wrap_content"  
                                android:layout_marginTop="10dp"  
                                android:columnWidth="90dp"   
                                android:stretchMode="columnWidth"  
                                android:numColumns="auto_fit"   
                                android:horizontalSpacing="10dp"  
                                android:verticalSpacing="10dp"   
                                android:gravity="center">  
                      </GridView>  
            </LinearLayout>

   (四) MainActivity.java文件代码:

public class MainActivity extends Activity {
          private GridView mGridView;   //MyGridView
          //定义图标数组
         private int[] imageRes = { R.drawable.png1, R.drawable.png2,
         R.drawable.png3, R.drawable.png4, R.drawable.png5, R.drawable.png6,
         R.drawable.png7, R.drawable.png8, R.drawable.png9,
         R.drawable.png10, R.drawable.png11, R.drawable.png12 };
         //定义标题数组
         private String[] itemName = { "审前调查", "需求评估", "在册人员", "请销假", "集中教育",
         "个别教育", "心理测评", "生活量表", "矫正方案", "矫正建议", "出勤统计", "综合查询" };

        @Override
        public void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  this.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Activity标题不显示
                  this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // 设置全屏显示
                  setContentView(R.layout.main);
   
                  mGridView = (GridView) findViewById(R.id.MyGridView);
                  List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
                  int length = itemName.length;
                  for (int i = 0; i < length; i++) {
                          HashMap<String, Object> map = new HashMap<String, Object>();
                          map.put("ItemImageView", imageRes[i]);
                          map.put("ItemTextView", itemName[i]);
                          data.add(map);
                  }
                  //为itme.xml添加适配器
                  SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this,
                  data, R.layout.item, new String[] { "ItemImageView","ItemTextView" }, new int[] { R.id.ItemImageView,R.id.ItemTextView });
                  mGridView.setAdapter(simpleAdapter);
                  //为mGridView添加点击事件监听器
                  mGridView.setOnItemClickListener(new GridViewItemOnClick());
        }
        //定义点击事件监听器
        public class GridViewItemOnClick implements OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) {
               Toast.makeText(getApplicationContext(), position + "",
               Toast.LENGTH_SHORT).show();
        }
     }
}