歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android基礎篇之底部菜單欄的編輯

Android基礎篇之底部菜單欄的編輯

日期:2017/3/1 10:50:19   编辑:Linux編程

先看效果圖: 如下

下面是代碼部分:

1. 在main.xml中添加GridView

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:Android="http://schemas.android.com/apk/res/android"
  3. android:background="@drawable/background"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <GridView
  8. android:id="@+id/grid_view"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_alignParentBottom="true"
  12. android:numColumns="5"
  13. android:horizontalSpacing="10dp"
  14. android:verticalSpacing="10dp">
  15. </GridView>
  16. </RelativeLayout>

2.注意:菜單欄中的每一子項都需要一個布局文件

如上圖 是一個垂直布局的LinearLayout

menu_item.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content">
  6. <ImageView
  7. android:id="@+id/item_iamge"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_gravity="center_horizontal"
  11. />
  12. <TextView
  13. android:id="@+id/item_text"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_gravity="center_horizontal"/>
  17. </LinearLayout>

3.主要代碼部分 MainActivity.java

  1. package com.yin.bottom_menu;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.widget.GridView;
  7. import android.widget.SimpleAdapter;
  8. public class MainActivity extends Activity {
  9. //圖片資源
  10. private static int[] images = {
  11. R.drawable.menu_add_new,
  12. R.drawable.menu_delete,
  13. R.drawable.menu_exit,
  14. R.drawable.menu_search,
  15. R.drawable.menu__list
  16. };
  17. //菜單欄中的文字顯示
  18. private static String[] menu_texts = {
  19. "增加","刪除","退出","查找","菜單"
  20. };
  21. private ArrayList<HashMap<String,Object>> menu_data ;
  22. private GridView grid_view;
  23. public void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.main);
  26. init_menu();
  27. }
  28. //初始化底部菜單欄
  29. private void init_menu(){
  30. grid_view = (GridView) findViewById(R.id.grid_view);
  31. add_menu_data();
  32. //向菜單欄中的控件添加適配其
  33. SimpleAdapter adapter = new SimpleAdapter(this, menu_data, R.layout.menu_item,
  34. new String[]{"menu_image","menu_text"}, new int[]{R.id.item_iamge,R.id.item_text});
  35. grid_view.setAdapter(adapter);
  36. }
  37. //添加菜單欄中顯示的數據
  38. private void add_menu_data(){
  39. menu_data = new ArrayList<HashMap<String,Object>>();
  40. for(int i=0;i<images.length;i++){
  41. HashMap<String,Object> map = new HashMap<String, Object>();
  42. map.put("menu_image", images[i]);
  43. map.put("menu_text", menu_texts[i]);
  44. menu_data.add(map);
  45. }
  46. }
  47. }
Copyright © Linux教程網 All Rights Reserved