Android之UI學習篇:使用GridView實現九宮格的菜單

來源:互聯網
上載者:User

今天在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>   (四) 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();        }     }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.