先看一張圖
這裡有幾個要點
1,主界面有個listview+下面一個toolbar
2,按menu鍵的時候彈出一個dialog
問題是,彈出的dialog如何顯示成gridview的形式呢,你說對了,就是用一個gridview來顯示出來。
具體的思路就是如此了,首先主界面運用一個RelativeLayout來布局相對的位置,上面是個ListView,下面是個Gridview,當然,你也可以用其他的方式來布局先面的toolbar。
新建一個項目,我的MainActivity名字是UcwebDemoActivity,代碼如下:
- public class UcwebDemoActivity extends Activity {
- AlertDialog menuDialog;
- ListView listView;
- GridView menuGrid, toolbarGrid;
- View menuView;
- private boolean isMore = false;
- private final int ITEM_SEARCH = 0;
- private final int ITEM_FILE_MANAGER = 1;
- private final int ITEM_DOWN_MANAGER = 2;
- private final int ITEM_FULLSCREEN = 3;
- private final int ITEM_MORE = 11;
-
- private final int TOOLBAR_ITEM_PAGEHOME = 0;
- private final int TOOLBAR_ITEM_BACK = 1;
- private final int TOOLBAR_ITEM_FORWARD = 2;
- private final int TOOLBAR_ITEM_NEW = 3;
- private final int TOOLBAR_ITEM_MENU = 4;
- int[] menu_image_array = { R.drawable.menu_search,
- R.drawable.menu_filemanager, R.drawable.menu_downmanager,
- R.drawable.menu_fullscreen, R.drawable.menu_inputurl,
- R.drawable.menu_bookmark, R.drawable.menu_bookmark_sync_import,
- R.drawable.menu_sharepage, R.drawable.menu_quit,
- R.drawable.menu_nightmode, R.drawable.menu_refresh,
- R.drawable.menu_more };
- String[] menu_name_array = { "Search", "Filemanager", "Download",
- "Fullscreen", "Inputurl", "Bookmark", "Bookmark_sync", "Share",
- "Quit", "NightModeʽ", "Refresh", "More" };
- int[] menu_image_array2 = { R.drawable.menu_auto_landscape,
- R.drawable.menu_penselectmodel, R.drawable.menu_page_attr,
- R.drawable.menu_novel_mode, R.drawable.menu_page_updown,
- R.drawable.menu_checkupdate, R.drawable.menu_checknet,
- R.drawable.menu_refreshtimer, R.drawable.menu_syssettings,
- R.drawable.menu_help, R.drawable.menu_about, R.drawable.menu_return };
- String[] menu_name_array2 = { "Landscape", "SelectModelʽ", "Pageʽ",
- "Modeʽ", "Updown", "Update", "CheckNet", "Refreshtimer", "Setting",
- "Help", "About", "Return" };
-
- int[] menu_toolbar_image_array = { R.drawable.controlbar_homepage,
- R.drawable.controlbar_backward_enable,
- R.drawable.controlbar_forward_enable, R.drawable.controlbar_window,
- R.drawable.controlbar_showtype_list };
- String[] menu_toolbar_name_array = { "Home", "Back", "Forward", "Window",
- "List" };
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- menuView = View.inflate(this, R.layout.gridview_menu, null);
- menuDialog = new AlertDialog.Builder(this).create();
- menuDialog.setView(menuView);
- menuDialog.setOnKeyListener(new OnKeyListener() {
- public boolean onKey(DialogInterface dialog, int keyCode,
- KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_MENU)// �����
- dialog.dismiss();
- return false;
- }
- });
-
- menuGrid = (GridView) menuView.findViewById(R.id.gridview);
- menuGrid.setAdapter(getMenuAdapter(menu_name_array, menu_image_array));
- menuGrid.setOnItemClickListener(new OnItemClickListener() {
- public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
- long arg3) {
- switch (arg2) {
- case ITEM_SEARCH:
-
- break;
- case ITEM_FILE_MANAGER:
-
- break;
- case ITEM_DOWN_MANAGER:
-
- break;
- case ITEM_FULLSCREEN:// ȫ��
-
- break;
- case ITEM_MORE:
- if (isMore) {
- menuGrid.setAdapter(getMenuAdapter(menu_name_array2,
- menu_image_array2));
- isMore = false;
- } else {
- menuGrid.setAdapter(getMenuAdapter(menu_name_array,
- menu_image_array));
- isMore = true;
- }
- menuGrid.invalidate();
- menuGrid.setSelection(ITEM_MORE);
- break;
- }
-
- }
- });
-
- toolbarGrid = (GridView) findViewById(R.id.GridView_toolbar);
- toolbarGrid.setBackgroundResource(R.drawable.channelgallery_bg);
- toolbarGrid.setNumColumns(5);
- toolbarGrid.setGravity(Gravity.CENTER);
- toolbarGrid.setVerticalSpacing(10);
- toolbarGrid.setHorizontalSpacing(10);
- toolbarGrid.setAdapter(getMenuAdapter(menu_toolbar_name_array,
- menu_toolbar_image_array));
- toolbarGrid.setOnItemClickListener(new OnItemClickListener() {
- public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
- long arg3) {
- Toast.makeText(UcwebDemoActivity.this,
- menu_toolbar_name_array[arg2], Toast.LENGTH_SHORT)
- .show();
- switch (arg2) {
- case TOOLBAR_ITEM_PAGEHOME:
- break;
- case TOOLBAR_ITEM_BACK:
-
- break;
- case TOOLBAR_ITEM_FORWARD:
-
- break;
- case TOOLBAR_ITEM_NEW:
-
- break;
- case TOOLBAR_ITEM_MENU:
- menuDialog.show();
- break;
- }
- }
- });
-
- listView = (ListView) findViewById(R.id.ListView_catalog);
- listView.setAdapter(getMenuAdapter(menu_name_array2, menu_image_array2));
-
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- menu.add("menu");
- return super.onCreateOptionsMenu(menu);
- }
-
- @Override
- public boolean onMenuOpened(int featureId, Menu menu) {
- if (menuDialog == null) {
- menuDialog = new AlertDialog.Builder(this).setView(menuView).show();
- } else {
- menuDialog.show();
- }
- return false;
- }
-
- private SimpleAdapter getMenuAdapter(String[] menuNameArray,
- int[] imageResourceArray) {
- ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
- for (int i = 0; i < menuNameArray.length; i++) {
- HashMap<String, Object> map = new HashMap<String, Object>();
- map.put("itemImage", imageResourceArray[i]);
- map.put("itemText", menuNameArray[i]);
- data.add(map);
- }
- SimpleAdapter simperAdapter = new SimpleAdapter(this, data,
- R.layout.item_menu, new String[] { "itemImage", "itemText" },
- new int[] { R.id.item_image, R.id.item_text });
- return simperAdapter;
- }
- }