当前位置:操作系统 > 安卓/Android >>

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"
     
接下来开始开发九宫格的菜单
开发出来的界面截图:
      
 
   开发步骤:
   (一) 放入图标资源到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>
   
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,