您的位置:首页 > 移动开发 > Android开发

(android地图开发) 高德地图添加自定义菜单栏

2013-08-15 11:01 691 查看
截图效果:






布局文件:(内涵相关布局文件注释)

<?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="wrap_content"
//底部 
    android:gravity="bottom"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/home"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/tab_one_normal"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/zhuye"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/home"
            //文本内容居中
android:gravity="center"
            android:paddingTop="34px"
            android:text="@string/home"
            android:textColor="@color/back"
            android:textSize="12dp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/publish"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/tab_one_normal"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/publish"
            android:gravity="center"
            android:paddingTop="34px"
            android:text="@string/publish"
            android:textColor="@color/back"
            android:textSize="12dp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/change"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/tab_one_normal"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/change"
            android:gravity="center"
            android:paddingTop="34px"
            android:text="@string/change"
            android:textColor="@color/back"
            android:textSize="12dp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/gn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/tab_one_normal"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/more"
            android:gravity="center"
            android:paddingTop="34px"
            android:text="@string/gn1"
            android:textColor="@color/back"
            android:textSize="12dp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/gn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/tab_one_normal"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/more"
            android:gravity="center"
            android:paddingTop="34px"
            android:text="@string/gn2"
            android:textColor="@color/back"
            android:textSize="12dp" />
    </LinearLayout>

</LinearLayout>


Activity相关代码:

//自定义弹出式菜单栏(相关组件)	
		private PopupWindow pop;
		//自定义菜单布局文件
		private LayoutInflater inflater ;
		//自定义菜单栏组件
		private View layout;
		//自定义菜单栏布局
		private LinearLayout home,publish,change,gn1,gn2;
		//按钮
		private Button zhuye;
		//相关统计
		private int i;
		//判断是否显示
		private boolean IsShow=true;


核心代码(实现自定义菜单栏,需要实现Activity的onKeyDown事件)

//自定义菜单栏的相关方法
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		  if(keyCode==KeyEvent.KEYCODE_MENU)
		     {
			   // /通过View.inflate加载不是onCreate加载的xml
		    	inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
		 		layout = inflater.inflate(R.layout.main,null);
		 		pop = new PopupWindow(layout,480,350);//获取PopupWindow对象并设置窗体的大小
		 		pop.showAtLocation(layout, Gravity.CENTER,20,470); //设置窗体的位置
		 		pop.showAsDropDown(layout); 	 		//弹出menu窗口	
		 		home=(LinearLayout)layout.findViewById(R.id.home);//找到弹出窗口上的控件
		 	    
		 	    home.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						//单击时,可通过设置IsShow的boolean值,使得单击时是一种颜色,
						//单击过后恢复单击前的颜色,不这么设置就会导致,
						//当你单击了和单击后都会一直显示单击时的背景色,不会有颜色变化。
						if(IsShow==true)
						{
						home.setBackgroundResource(R.drawable.tab_two_highlight);
						publish.setBackgroundResource(R.drawable.tab_one_normal);
						change.setBackgroundResource(R.drawable.tab_one_normal);
						gn1.setBackgroundResource(R.drawable.tab_one_normal);	
						gn2.setBackgroundResource(R.drawable.tab_one_normal);		
						IsShow=false;
						}
						else
						{
							home.setBackgroundResource(R.drawable.tab_one_normal);
							publish.setBackgroundResource(R.drawable.tab_one_normal);
							change.setBackgroundResource(R.drawable.tab_one_normal);
							gn1.setBackgroundResource(R.drawable.tab_one_normal);	
							gn2.setBackgroundResource(R.drawable.tab_one_normal);			
							IsShow=true;
						}
					}
				});
		 		publish=(LinearLayout)layout.findViewById(R.id.publish);
		 		publish.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						if(IsShow==false)
						{
						publish.setBackgroundResource(R.drawable.tab_two_highlight);
						home.setBackgroundResource(R.drawable.tab_one_normal);
						change.setBackgroundResource(R.drawable.tab_one_normal);
						gn1.setBackgroundResource(R.drawable.tab_one_normal);
						gn2.setBackgroundResource(R.drawable.tab_one_normal);			
						IsShow=true;
						}
						else
						{
							publish.setBackgroundResource(R.drawable.tab_one_normal);
							home.setBackgroundResource(R.drawable.tab_one_normal);
							change.setBackgroundResource(R.drawable.tab_one_normal);
							gn1.setBackgroundResource(R.drawable.tab_one_normal);
							gn2.setBackgroundResource(R.drawable.tab_one_normal);
							IsShow=false;
						}
					}
				});
		 		
		 		change=(LinearLayout)layout.findViewById(R.id.change);
		 		change.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						if(IsShow==true)
						{
						change.setBackgroundResource(R.drawable.tab_two_highlight);
						home.setBackgroundResource(R.drawable.tab_one_normal);
						publish.setBackgroundResource(R.drawable.tab_one_normal);
						gn1.setBackgroundResource(R.drawable.tab_one_normal);
						gn2.setBackgroundResource(R.drawable.tab_one_normal);	
						IsShow=false;
						}
						else
						{
							change.setBackgroundResource(R.drawable.tab_one_normal);
							home.setBackgroundResource(R.drawable.tab_one_normal);
							publish.setBackgroundResource(R.drawable.tab_one_normal);
							gn1.setBackgroundResource(R.drawable.tab_one_normal);	
							gn2.setBackgroundResource(R.drawable.tab_one_normal);		
							IsShow=true;
						}
					}
				});
		 		
		 		gn1=(LinearLayout)layout.findViewById(R.id.gn1);
		 		gn1.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						if(IsShow==false)
						{
							gn1.setBackgroundResource(R.drawable.tab_two_highlight);
							home.setBackgroundResource(R.drawable.tab_one_normal);
							publish.setBackgroundResource(R.drawable.tab_one_normal);
							change.setBackgroundResource(R.drawable.tab_one_normal);   
							gn2.setBackgroundResource(R.drawable.tab_one_normal);   
							IsShow=true;
						}
						else
						{
							gn1.setBackgroundResource(R.drawable.tab_one_normal);
							home.setBackgroundResource(R.drawable.tab_one_normal);
							publish.setBackgroundResource(R.drawable.tab_one_normal);
							change.setBackgroundResource(R.drawable.tab_one_normal);
							gn2.setBackgroundResource(R.drawable.tab_one_normal);
							IsShow=false;
						}
					}
				});	
		 		
				gn2=(LinearLayout)layout.findViewById(R.id.gn2);
		 		gn2.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						if(IsShow==false)
						{
							gn2.setBackgroundResource(R.drawable.tab_two_highlight);
							home.setBackgroundResource(R.drawable.tab_one_normal);
							publish.setBackgroundResource(R.drawable.tab_one_normal);
							change.setBackgroundResource(R.drawable.tab_one_normal);   
							gn1.setBackgroundResource(R.drawable.tab_one_normal);   
							IsShow=true;
						}
						else
						{
							gn2.setBackgroundResource(R.drawable.tab_one_normal);
							home.setBackgroundResource(R.drawable.tab_one_normal);
							publish.setBackgroundResource(R.drawable.tab_one_normal);
							change.setBackgroundResource(R.drawable.tab_one_normal);
							gn1.setBackgroundResource(R.drawable.tab_one_normal);
							IsShow=false;
						}
					}
				});	
	            return false;
		     }
	 
		     if(keyCode==KeyEvent.KEYCODE_BACK)
		     {
		    	 //是当前页即没弹出菜单窗口的时候
		    	 if(i==1)
		    	 {
		    		 System.exit(0);	 
		    		 i=2;
		    	 }
		    	 //弹出菜单窗口的时候
		    	 else
		    	 {
		    		 //弹出窗口关闭
		    		 pop.dismiss();
		    		 i=1;
		    	 }
		    	
		 	    return false;
		     }
			return false;
	}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: