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

本文介绍了一种自定义菜单栏的设计与实现方法,利用Android的PopupWindow组件结合LinearLayout布局,实现了响应菜单键弹出菜单,并通过点击不同选项改变背景色的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

截图效果:

               

 

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

<?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;
	}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值