前言:

  学习android断断续续也有一年半左右,但一直在学习,很少回顾以往的知识。所以我打算用业余时间来写一些这样总结性的文章,希望温故知新。

  以下只是我个人的一些感悟和见解(当然会查证资料验证),难免有错误之处。所以如果有不同意见,十分欢迎留言交流,再次拜谢。

简介:

  首先,此处的菜单不是Menu.它是各类菜单和导航的统称。这里包括了android的Menu、tab、slidingMenu、通过ViewPager和TitlePageIndicator的导航、自定义menu等等。

  咱们可以定义,菜单布局=选项区+内容区。

  选项区:

    选项区基本有以下几种:

      一,固定在屏幕顶部或者底部。

        

      二,隐藏在屏幕边。

         

      三,按下手机菜单键出现和隐藏。

        (这里是应用重写了Menu键的响应,改用了自己的popwindow)

      

      总的来说,选项区就是一些在排列上有组织的widget.

  内容区

      内容区大致可以分为两种:

      一种是“一个容器型”。容器可以是一个view(通常是一个空的布局),然后每次内容改变时,通过LayoutInflater获取一个布局的实例化或者是通过findViewById获取一个widget,返回值是view,因此直接赋值或者addView都可以。

      另一种是“多个容器型”。比如多个Activity,你没选择一项就跳转到另一个Activity.(tabActivity虽然addTab()时的setContent()方法可以传Intent,但实际上都是把每个Activity放入tabActivity局部中id为tabCotent的组件中显示,所以也可以认为是采用了“一个容器型”,虽然数据的传输上和多个Activity之间的传输一致。)

      总的来说,内容区就是一个或多个可以往里面放View的容器。

从零开始实现这些菜单:

  版本一:

      用最简单的思维看,选项区的特点是有组织,然后普遍的做法是给选中的选项加上标识(win8风格就无须这个),这就还需要选项之间互斥。要实现这些,很简单,几个Button就可以搞定。

      1,用LinearLayout把这些Button装起来。这里注意:LinearLayout的gravity只能作用于垂直当前方向的方向,即orientation为vertical的布局,layout_gravity="left"有效,layout_gravity="bottom"无效.我这里用一个LinearLayout将其挤压到底部。刚好这个LinearLayout用来放置内容区。布局如下:

      

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <LinearLayout
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical"
6 xmlns:android="http://schemas.android.com/apk/res/android">
7 <LinearLayout
8 android:id="@+id/main_tabcontent"
9 android:layout_width="fill_parent"
10 android:layout_height="fill_parent"
11 android:layout_weight="1"/>
12 <LinearLayout
13 android:layout_width="fill_parent"
14 android:layout_height="70dp"
15 android:orientation="horizontal">
16 <ImageButton
17 android:id="@+id/main_tab_wt"
18 android:layout_width="fill_parent"
19 android:layout_height="fill_parent"
20 android:layout_weight="1"
21 android:background="@drawable/main_tab_wt_1"/>
22 <ImageButton
23 android:id="@+id/main_tab_qz"
24 android:layout_width="fill_parent"
25 android:layout_height="fill_parent"
26 android:layout_weight="1"
27 android:background="@drawable/main_tab_qz_0"/>
28 <ImageButton
29 android:id="@+id/main_tab_yh"
30 android:layout_width="fill_parent"
31 android:layout_height="fill_parent"
32 android:layout_weight="1"
33 android:background="@drawable/main_tab_yh_0"/>
34 <ImageButton
35 android:id="@+id/main_tab_sc"
36 android:layout_width="fill_parent"
37 android:layout_height="fill_parent"
38 android:layout_weight="1"
39 android:background="@drawable/main_tab_home_0"/>
40
41 </LinearLayout>
42
43 </LinearLayout>

      2,在OnClickListener中监听每个Button的点击,当一个Button被点击后,更改这些Button的背景实现互斥。然后每个Button对应的内容也在监听器中贴入View。

贴入布局的最简单方法是什么?当然是直接获取XML布局的实例了。代码如下(我写的逻辑部分临时给删了,请无视那些多余的import吧):

package com.zhihu.ui;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Set; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import com.zhihu.model.ObjectToXml;
import com.zhihu.model.Question;
import com.zhihu.model.QuestionHelper;
import com.zhihu.model.WtAdapter;
import com.zhihu.model.cache; import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import android.support.v4.app.NavUtils; public class MainActivity extends Activity implements OnClickListener{
private View wtView,qzView,scView,yhView;
private ImageButton wtImage,qzImage,scImage,yhImage;
private LayoutInflater layoutInflater;
private LinearLayout tabContent; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
initViews();
} private void initViews() {
// TODO Auto-generated method stub
wtImage.setOnClickListener(this);
qzImage.setOnClickListener(this);
scImage.setOnClickListener(this);
yhImage.setOnClickListener(this);
} private void findViews() {
// TODO Auto-generated method stub
//view of mainActivity
layoutInflater = getLayoutInflater().from(this);
tabContent = (LinearLayout) findViewById(R.id.main_tabcontent);
wtImage = (ImageButton) findViewById(R.id.main_tab_wt);
qzImage = (ImageButton) findViewById(R.id.main_tab_qz);
scImage = (ImageButton) findViewById(R.id.main_tab_sc);
yhImage = (ImageButton) findViewById(R.id.main_tab_yh); //获取布局的实例化
wtView = layoutInflater.inflate(R.layout.main_wt, null);
qzView = layoutInflater.inflate(R.layout.main_qz, null);
scView = layoutInflater.inflate(R.layout.main_sc, null);
yhView = layoutInflater.inflate(R.layout.main_yh, null);
//初始化为显示第一个选项的内容
tabContent.addView(wtView);
} @Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.main_tab_wt:
wtImage.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_wt_1));
qzImage.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_qz_0));
scImage.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_home_0));
yhImage.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_yh_0));
tabContent.removeAllViews();
tabContent.addView(wtView);
break;
case R.id.main_tab_qz:
wtImage.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_wt_0));
qzImage.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_qz_1));
scImage.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_home_0));
yhImage.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_yh_0));
tabContent.removeAllViews();
tabContent.addView(qzView);
break;
case R.id.main_tab_sc:
wtImage.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_wt_0));
qzImage.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_qz_0));
scImage.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_home_1));
yhImage.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_yh_0));
tabContent.removeAllViews();
tabContent.addView(scView);
break;
case R.id.main_tab_yh:
wtImage.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_wt_0));
qzImage.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_qz_0));
scImage.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_home_0));
yhImage.setBackgroundDrawable(getResources().getDrawable(R.drawable.main_tab_yh_1));
tabContent.removeAllViews();
tabContent.addView(yhView);
break;
}
} }

       到了这步,基本完成。注意一点,在别的布局文件中的widget,findViewById时需要确定使用谁的方法。即应该用view.findViewById()。

       第一个版本基本OK,除去代码层面不说,功能上已经可以使用了。

       肯定有朋友会问,那四个Button为啥不用RadioButton来实现互斥呢?这个是下一个版本的事了。敬请关注。

Android菜单代码的更多相关文章

  1. Android菜单详解(一)——理解android中的Menu

    前言 今天看了pro android 3中menu这一章,对Android的整个menu体系有了进一步的了解,故整理下笔记与大家分享. PS:强烈推荐<Pro Android 3>,是我至 ...

  2. 【转】Android菜单详解——理解android中的Menu--不错

    原文网址:http://www.cnblogs.com/qingblog/archive/2012/06/08/2541709.html 前言 今天看了pro android 3中menu这一章,对A ...

  3. Android 常用代码大集合 [转]

    [Android]调用字符串资源的几种方法   字符串资源的定义 文件路径:res/values/strings.xml 字符串资源定义示例: <?xml version="1.0&q ...

  4. material design 的android开源代码整理

    material design 的android开源代码整理 1 android (material design 效果的代码库) 地址请点击:MaterialDesignLibrary 效果: 2 ...

  5. 简单的 Android 菜单

    Android 创建简单的菜单 一:上下文菜单: 1.在 res 下创建菜单项资源文夹 menu app->右击res->new->android resourse director ...

  6. Android菜单(动画菜单、360波纹菜单)

     Android菜单(动画菜单.360波纹菜单) 前言:Android菜单常用集合:FragmentTabHost系统菜单.上移式菜单.360波纹菜单.展开式菜单.详解注释,可直接拿来用! 效果:   ...

  7. 移动开发:美团外卖Android Lint代码检查实践

    概述 Lint是Google提供的Android静态代码检查工具,可以扫描并发现代码中潜在的问题,提醒开发人员及早修正,提高代码质量.除了Android原生提供的几百个Lint规则,还可以开发自定义L ...

  8. Android菜单

    Android菜单概述 菜单是Activity的一个重要组成部分,它为用户操作提供了快捷的途径.Android提供了一个简单的框架来向程序中添加标准菜单 . 一.创建一个菜单资源 你需要在一个XML ...

  9. Android菜单(menu)

    Android  菜单 我们继续来进行学习,今天写一下在软件中用的还算较多的菜单. 1.Menu 菜单,很显然,作用就是点击不同的选项触发不同的方法.现在在安卓使用中推荐使用ActionBar,但这里 ...

随机推荐

  1. sprak pom

    <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven ...

  2. 7.Java web—tomcat9部署

    1)安装 在此之前要安装 好jdk和jre 下载绿色版 http://tomcat.apache.org/ 解压至:D:\Program Files (x86)\tomcat9 环境变更path添加两 ...

  3. centos 安装php缓存 apc或zend-opcode

    去官方下载apc:pecl.php.net 搜索apc,安装最新的. #wget http://pecl.php.net/get/APC# tar -xzvf APC-3.1.9.tgz#cd  AP ...

  4. PHP用CURL发送Content-type为application/json的HTTP/HTTPS请求

    <?php $headers = array( "Content-type: application/json;charset='utf-8'", "Accept: ...

  5. 【kotlin】报错 Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type List<String>?

    报错如下: 解决如下: 另一种情况: 解决如下:

  6. wiki平台工具

    1.  confluence  评点: 好用,与world类似.模板多.

  7. 修改ubuntu系统时区

    ubuntu默认时区是Etc/UTC,和我们的北京时间相差8个时区,需要修改系统的时区,以下有两种简单方式修改系统时区: 1.修改/etc/timezone文件 vi /etc/timezone 把E ...

  8. POJ训练计划1035_Spell checker(串处理/暴力)

    Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18418   Accepted: 6759 De ...

  9. C# 通过比对哈希码判断两个文件内容是否相同

    1.使用System.security.Cryptography.HashAlgorithm类为每个文件生成一个哈希码,然后比较两个哈希码是否一致. 2. 在比较文件内容的时候可以采用好几种方法.例如 ...

  10. 性能问题案例02——sybase连接堵塞问题

    现象:近期现场反馈一个问题.系统在审批的时候,常常卡死.整个系统全然用不了,浏览器訪问处于loading的状态. 排查: 1.一般系统挂了首先想到内存问题,可是现象是loading,也就是说没有挂,线 ...