亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

android 自定義菜單

系統 1816 0

?

在android開發的過程中系統自帶的菜單往往滿足不了開發中的一些需求,比如說一排最多只能放置三個菜單,坐多只能放置6個,再多的話就會折疊起來,如果我們想再一排顯示4個或5個菜單那么就要自己想辦法處理。

這里我用布局的隱藏并加上動畫來模擬菜單的效果。

要點:

1、隱藏和顯示菜單,我使用了一個線性布局把菜單封裝起來。

          <?xml version="
          
            1.0
          
          " encoding="
          
            utf-8
          
          "?>
<LinearLayout xmlns:android="
          
            http://schemas.android.com/apk/res/android
          
          "
    android:layout_alignParentBottom="
          
            true
          
          "
    android:background="
          
            @drawable/menubackground
          
          "
    android:layout_width="
          
            fill_parent
          
          "
    android:layout_height="
          
            144px
          
          "
    android:orientation="
          
            vertical
          
          "
    android:gravity="
          
            center
          
          "
    android:visibility="
          
            gone
          
          "
  	android:id="
          
            @+id/lines
          
          ">
  	<LinearLayout android:orientation="
          
            horizontal
          
          " 
  	android:gravity="
          
            center
          
          " 
  	android:layout_width="
          
            fill_parent
          
          "
    android:layout_height="
          
            72px
          
          "
  	>
    <ImageButton
    	android:layout_marginLeft="
          
            8dip
          
          " 
    	android:id="
          
            @+id/menu_btn_index
          
          "
    	android:layout_width="
          
            wrap_content
          
          "
    	android:layout_height="
          
            wrap_content
          
          "
    	android:background="
          
            @drawable/menu_index_selector
          
          "
    	/>
    	
     <ImageButton
    	android:layout_width="
          
            wrap_content
          
          "
    	android:layout_height="
          
            wrap_content
          
          "
    	android:background="
          
            @drawable/menu_news_selector
          
          "
    	android:id="
          
            @+id/menu_btn_news
          
          "
    	/>
    	
     <ImageButton
    	android:layout_width="
          
            wrap_content
          
          "
    	android:layout_height="
          
            wrap_content
          
          "
    	android:background="
          
            @drawable/menu_lib_selector
          
          "
    	android:id="
          
            @+id/menu_btn_lib
          
          "
    	/>
    	 <ImageButton
    	android:layout_width="
          
            wrap_content
          
          "
    	android:layout_height="
          
            wrap_content
          
          "
    	android:background="
          
            @drawable/menu_add_selector
          
          "
    	android:id="
          
            @+id/menu_btn_add
          
          "
    	/>
    	
    <ImageButton
    	android:layout_width="
          
            wrap_content
          
          "
    	android:layout_height="
          
            wrap_content
          
          "
    	android:background="
          
            @drawable/menu_set_selector
          
          "
    	android:id="
          
            @+id/menu_btn_set
          
          "
    	/>
    
    </LinearLayout>
    
    <LinearLayout android:orientation="
          
            horizontal
          
          " android:gravity="
          
            center
          
          "
    	android:layout_width="
          
            fill_parent
          
          "
    	android:layout_height="
          
            72px
          
          ">
    <ImageButton
    	android:layout_marginLeft="
          
            8dip
          
          " 
    	android:id="
          
            @+id/menu_btn_index
          
          "
    	android:layout_width="
          
            wrap_content
          
          "
    	android:layout_height="
          
            wrap_content
          
          "
    	android:background="
          
            @drawable/menu_index_selector
          
          "
    	/>
    	
     <ImageButton
    	android:layout_width="
          
            wrap_content
          
          "
    	android:layout_height="
          
            wrap_content
          
          "
    	android:background="
          
            @drawable/menu_news_selector
          
          "
    	android:id="
          
            @+id/menu_btn_news
          
          "
    	/>
    	
     <ImageButton
    	android:layout_width="
          
            wrap_content
          
          "
    	android:layout_height="
          
            wrap_content
          
          "
    	android:background="
          
            @drawable/menu_lib_selector
          
          "
    	android:id="
          
            @+id/menu_btn_lib
          
          "
    	/>
    	
     <ImageButton
    	android:layout_width="
          
            wrap_content
          
          "
    	android:layout_height="
          
            wrap_content
          
          "
    	android:background="
          
            @drawable/menu_add_selector
          
          "
    	android:id="
          
            @+id/menu_btn_add
          
          "
    	/>
    	
    <ImageButton
    	android:layout_width="
          
            wrap_content
          
          "
    	android:layout_height="
          
            wrap_content
          
          "
    	android:background="
          
            @drawable/menu_quit_selector
          
          "
    	android:id="
          
            @+id/menu_btn_quit
          
          "
    	/>
    </LinearLayout>    	
</LinearLayout>


        

2、模擬菜單的效果,增加動畫,布局顯示的時候增加一個漸漸底部生氣的效果,隱藏的時候增加一個緩緩下落的效果,顯示菜單動畫文件:

          
            <?
          
          xml version="1.0" encoding="utf-8"
          
            ?>
          
          
            <
          
          
            set
          
          
            xmlns
          
          :
          
            android
          
          =
          
            "http://schemas.android.com/apk/res/android"
          
          
            >
          
          
            <
          
          
            translate
          
          
            android
          
          :
          
            fromXDelta
          
          =
          
            "0"
          
          
            android
          
          :
          
            toXDelta
          
          =
          
            "0"
          
          
            android
          
          :
          
            fromYDelta
          
          =
          
            "00"
          
          
            android
          
          :
          
            toYDelta
          
          =
          
            "140"
          
          
            android
          
          :
          
            duration
          
          =
          
            "200"
          
          
            />
          
          
            </
          
          
            set
          
          
            >
          
        

?

隱藏菜單動畫文件:

?

          
            <?
          
          xml version="1.0" encoding="utf-8"
          
            ?>
          
          
            <
          
          
            set
          
          
            xmlns
          
          :
          
            android
          
          =
          
            "http://schemas.android.com/apk/res/android"
          
          
            >
          
          
            <
          
          
            translate
          
          
            android
          
          :
          
            fromXDelta
          
          =
          
            "0"
          
          
            android
          
          :
          
            toXDelta
          
          =
          
            "0"
          
          
            android
          
          :
          
            fromYDelta
          
          =
          
            "140"
          
          
            android
          
          :
          
            toYDelta
          
          =
          
            "00"
          
          
            android
          
          :
          
            duration
          
          =
          
            "200"
          
          
            />
          
          
            </
          
          
            set
          
          
            >
          
        


動畫調用:

?

?

          
            /**
     * 顯示菜單欄, 重新實現的Option menu.
     * */
          
          
            private
          
          
            void
          
           showAppMenu() {
    	
          
            if
          
           (menuShowAnimation == 
          
            null
          
          ) {
    		menuShowAnimation = AnimationUtils
    				.loadAnimation(mContext, R.anim.menuhide);
    	}
    	myLayout.startAnimation(menuShowAnimation);
    	myLayout.setVisibility(View.VISIBLE);
    }

    
          
            /**
     * 隱藏菜單欄, 重新實現的Option menu.
     * */
          
          
            private
          
          
            void
          
           hideAppMenu() {
    	myLayout.setVisibility(View.GONE);
    	
          
            if
          
           (menuHideAnimation == 
          
            null
          
          )
    		menuHideAnimation =AnimationUtils
    				.loadAnimation(mContext, R.anim.menushow);
    	myLayout.startAnimation(menuHideAnimation);
    }
        


3、控制菜單的隱藏和顯示,需要重寫三個方法public boolean onCreateOptionsMenu(Menu menu),

public boolean dispatchKeyEvent(KeyEvent event) 和public boolean dispatchTouchEvent(MotionEvent event)

?

          @Override
	
          
            public
          
          
            boolean
          
           onCreateOptionsMenu(Menu menu) {
		
          
            if
          
          (mCustomMenu==
          
            null
          
          )
			mCustomMenu=
          
            new
          
           CustomMenu(CustomMenuActivity.
          
            this
          
          ,CustomMenuActivity.
          
            this
          
          );
		mCustomMenu.CreateMenu();
		
          
            return
          
          
            false
          
          ;
	}
	
	@Override
	
          
            public
          
          
            boolean
          
           dispatchKeyEvent(KeyEvent event) {
		
          
            if
          
          (mCustomMenu!=
          
            null
          
          )
			
          
            return
          
           mCustomMenu.dispatchKeyEvent(event,
          
            super
          
          .dispatchKeyEvent(event));
		
          
            return
          
          
            super
          
          .dispatchKeyEvent(event);
	}
	
	@Override
	
          
            public
          
          
            boolean
          
           dispatchTouchEvent(MotionEvent event) {
		
          
            if
          
          (mCustomMenu!=
          
            null
          
          )
			
          
            return
          
           mCustomMenu.dispatchTouchEvent(event,
          
            super
          
          .dispatchTouchEvent(event));
		
          
            return
          
          
            super
          
          .dispatchTouchEvent(event);
	}
        


4、實現菜單點擊時候被點擊菜單狀態的般變化,這里我使用了selector來實現,菜單我使用ImageButton將selector賦值給ImageButton 的background即可:

一個菜單項

          
            <
          
          
            ImageButton
          
          
            android
          
          :
          
            layout_marginLeft
          
          =
          
            "8dip"
          
          
            android
          
          :
          
            id
          
          =
          
            "@+id/menu_btn_index"
          
          
            android
          
          :
          
            layout_width
          
          =
          
            "wrap_content"
          
          
            android
          
          :
          
            layout_height
          
          =
          
            "wrap_content"
          
          
            android
          
          :
          
            background
          
          =
          
            "@drawable/menu_index_selector"
          
          
            />
          
        

menu_index_selector 文件內容如下:

          
            <?
          
          xml version="1.0" encoding="utf-8"
          
            ?>
          
          
            <!-- Copyright (C) 2009 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
          
          
            <
          
          
            selector
          
          
            xmlns
          
          :
          
            android
          
          =
          
            "http://schemas.android.com/apk/res/android"
          
          
            >
          
          
            <
          
          
            item
          
          
            android
          
          :
          
            state_window_focused
          
          =
          
            "false"
          
          
            android
          
          :
          
            drawable
          
          =
          
            "@drawable/menu_index"
          
          
            />
          
          
            <
          
          
            item
          
          
            android
          
          :
          
            state_pressed
          
          =
          
            "true"
          
          
            android
          
          :
          
            drawable
          
          =
          
            "@drawable/menu_index1"
          
          
            />
          
          
            <
          
          
            item
          
          
            android
          
          :
          
            state_focused
          
          =
          
            "true"
          
          
            android
          
          :
          
            drawable
          
          =
          
            "@drawable/menu_index1"
          
          
            />
          
          
            <
          
          
            item
          
          
            android
          
          :
          
            drawable
          
          =
          
            "@drawable/menu_index"
          
          
            />
          
          
            </
          
          
            selector
          
          
            >
          
        

        ?
      

5、頁面的調用使用:<include>標簽來進行引用:

?

          
            <?
          
          xml version="1.0" encoding="utf-8"
          
            ?>
          
          
            <
          
          
            RelativeLayout
          
          
            xmlns
          
          :
          
            android
          
          =
          
            "http://schemas.android.com/apk/res/android"
          
          
            android
          
          :
          
            orientation
          
          =
          
            "vertical"
          
          
            android
          
          :
          
            layout_width
          
          =
          
            "fill_parent"
          
          
            android
          
          :
          
            layout_height
          
          =
          
            "fill_parent"
          
          
            >
          
          
            <
          
          
            include
          
          
            layout
          
          =
          
            "@layout/menu_layout"
          
          
            />
          
          
            </
          
          
            RelativeLayout
          
          
            >
          
        

這樣的話一個模擬的自定義菜單就基本完成了,菜單控制完整代碼java類:

        
          package
        
         com.demo.utils;


        
          import
        
         android.app.Activity;

        
          import
        
         android.content.Context;

        
          import
        
         android.content.res.Resources;

        
          import
        
         android.view.KeyEvent;

        
          import
        
         android.view.MotionEvent;

        
          import
        
         android.view.View;

        
          import
        
         android.view.View.OnClickListener;

        
          import
        
         android.view.animation.Animation;

        
          import
        
         android.view.animation.AnimationUtils;

        
          import
        
         android.widget.ImageButton;

        
          import
        
         android.widget.LinearLayout;


        
          import
        
         com.demo.HelloWorld.R;


        
          /**
 * @author Administrator
 *         xsl  xushilin@kingtoneinfo.com
 * @version: 創建時間:2011-8-30 上午11:16:19
 * 說         明:
 * 修改歷史:
 */
        
        
          public
        
        
          class
        
         CustomMenu {
	
        
          private
        
         LinearLayout myLayout;
	
        
          private
        
          Context mContext;
	
        
          private
        
         Activity mActivity;
	
        
          private
        
         Animation menuShowAnimation = 
        
          null
        
        ;
	
        
          private
        
         Animation menuHideAnimation = 
        
          null
        
        ;
	
        
          private
        
         Resources res;
	
        
          public
        
        
          int
        
         screen_height;
	
        
          private
        
         ImageButton imgIndex,imgSet,imgNews,imgAdd,imgQuit,imgLib;	
	
        
          public
        
         CustomMenu(Context context,Activity activity){
		
		mContext=context;	
		mActivity=activity;
		res = mContext.getResources();
		screen_height = res.getDisplayMetrics().heightPixels;
		myLayout=(LinearLayout)activity.findViewById(R.id.lines);
		
		imgIndex=(ImageButton)activity.findViewById(R.id.menu_btn_index);	
		imgSet=(ImageButton)activity.findViewById(R.id.menu_btn_set);
		imgNews=(ImageButton)activity.findViewById(R.id.menu_btn_news);
		imgAdd=(ImageButton)activity.findViewById(R.id.menu_btn_add);
		imgQuit=(ImageButton)activity.findViewById(R.id.menu_btn_quit);
		imgLib=(ImageButton)activity.findViewById(R.id.menu_btn_lib);

		
        
          //返回首頁
        
        
		imgIndex.setOnClickListener(
        
          new
        
         OnClickListener(){
			
        
          public
        
        
          void
        
         onClick(View v) {
				
        
          //TODO do somthing
        
        
			}			
		});
		
        
          //設置
        
        
		imgSet.setOnClickListener(
        
          new
        
         OnClickListener(){
			
        
          public
        
        
          void
        
         onClick(View v) {
				
        
          //TODO do somthing
        
        
			}			
		});
		
        
          //查詢
        
        
		imgNews.setOnClickListener(
        
          new
        
         OnClickListener(){
			
        
          public
        
        
          void
        
         onClick(View v) {
				
        
          //TODO do somthing
        
        
			}			
		});
		
        
          //編輯
        
        
		imgAdd.setOnClickListener(
        
          new
        
         OnClickListener(){
			
        
          public
        
        
          void
        
         onClick(View v) {
				
        
          //TODO do somthing
        
        
			}			
		});
		
        
          //退出系統
        
        
		imgQuit.setOnClickListener(
        
          new
        
         OnClickListener(){
			
        
          public
        
        
          void
        
         onClick(View v) {
				
        
          //TODO do somthing
        
        
			}			
		});
		
        
          //素材庫
        
        
		imgLib.setOnClickListener(
        
          new
        
         OnClickListener(){
			
        
          public
        
        
          void
        
         onClick(View v) {
				
        
          //TODO do somthing
        
        
			}			
		});
	}
	
        
          public
        
        
          void
        
          CreateMenu(){		
		
        
          if
        
        (myLayout.getVisibility()==View.GONE)
			showAppMenu();
			
        
          //myLayout.setVisibility(View.VISIBLE);		
        
        
          else
        
        
			hideAppMenu();
			
        
          //myLayout.setVisibility(View.GONE);
        
        
	}
	  
        
          /**
     * 顯示菜單欄, 重新實現的Option menu.
     * */
        
        
          private
        
        
          void
        
         showAppMenu() {
    	
        
          if
        
         (menuShowAnimation == 
        
          null
        
        ) {
    		menuShowAnimation = AnimationUtils
    				.loadAnimation(mContext, R.anim.menuhide);
    	}
    	myLayout.startAnimation(menuShowAnimation);
    	myLayout.setVisibility(View.VISIBLE);
    }

    
        
          /**
     * 隱藏菜單欄, 重新實現的Option menu.
     * */
        
        
          private
        
        
          void
        
         hideAppMenu() {
    	myLayout.setVisibility(View.GONE);
    	
        
          if
        
         (menuHideAnimation == 
        
          null
        
        )
    		menuHideAnimation =AnimationUtils
    				.loadAnimation(mContext, R.anim.menushow);
    	myLayout.startAnimation(menuHideAnimation);
    }
    
    
    
        
          public
        
        
          boolean
        
         dispatchTouchEvent(MotionEvent event,
        
          boolean
        
         b) {
		
        
          if
        
         (myLayout.getVisibility() == View.VISIBLE) {
			
        
          int
        
         y = (
        
          int
        
        ) event.getRawY();
			
        
          if
        
         (y < screen_height - myLayout.getHeight()) {
				hideAppMenu();
				
        
          return
        
        
          true
        
        ;
			}
		}    	
		
        
          return
        
         b;
    }
    
    
        
          public
        
        
          boolean
        
         dispatchKeyEvent(KeyEvent event,
        
          boolean
        
         b) {
    	
        
          int
        
         act = event.getAction();
    	
        
          int
        
         code = event.getKeyCode();        	
    	
        
          // app menu like option menu
        
        
          if
        
         (code == KeyEvent.KEYCODE_MENU){
	    	
        
          if
        
         (act == KeyEvent.ACTION_DOWN){
	    		
        
          if
        
         (myLayout.getVisibility() == View.VISIBLE) {
	    			hideAppMenu();
	    		} 
        
          else
        
         {
	    			showAppMenu();
	    		}
	    		
        
          return
        
        
          true
        
        ;
	    	}
    	}
        
          else
        
        
          if
        
         (code == KeyEvent.KEYCODE_BACK){
    		
        
          if
        
         (myLayout.getVisibility() == View.VISIBLE) {
    			hideAppMenu();
    			
        
          return
        
        
          true
        
        ;
    		}
    	}
    	
        
          return
        
         b;	
    }
}
      

activity調用菜單完整代碼:

        
          package
        
         com.demo.ui;


        
          import
        
         com.demo.HelloWorld.R;

        
          import
        
         com.demo.utils.CustomMenu;


        
          import
        
         android.app.Activity;

        
          import
        
         android.os.Bundle;

        
          import
        
         android.view.KeyEvent;

        
          import
        
         android.view.Menu;

        
          import
        
         android.view.MotionEvent;


        
          /**
 * @author XSL
 *         xsl  xushilin@kingtoneinfo.com
 * @version: 創建時間:2011-8-30 上午11:13:14
 * 說         明:
 * 修改歷史:
 */
        
        
          public
        
        
          class
        
         CustomMenuActivity 
        
          extends
        
         Activity {
	
        
          private
        
         CustomMenu mCustomMenu=
        
          null
        
        ;
	@Override
	
        
          public
        
        
          void
        
         onCreate(Bundle savedInstanceState) {
		
        
          super
        
        .onCreate(savedInstanceState);
		setContentView(R.layout.custom_menu);		
	}
	@Override
	
        
          public
        
        
          boolean
        
         onCreateOptionsMenu(Menu menu) {
		
        
          if
        
        (mCustomMenu==
        
          null
        
        )
			mCustomMenu=
        
          new
        
         CustomMenu(CustomMenuActivity.
        
          this
        
        ,CustomMenuActivity.
        
          this
        
        );
		mCustomMenu.CreateMenu();
		
        
          return
        
        
          false
        
        ;
	}
	
	@Override
	
        
          public
        
        
          boolean
        
         dispatchKeyEvent(KeyEvent event) {
		
        
          if
        
        (mCustomMenu!=
        
          null
        
        )
			
        
          return
        
         mCustomMenu.dispatchKeyEvent(event,
        
          super
        
        .dispatchKeyEvent(event));
		
        
          return
        
        
          super
        
        .dispatchKeyEvent(event);
	}
	
	@Override
	
        
          public
        
        
          boolean
        
         dispatchTouchEvent(MotionEvent event) {
		
        
          if
        
        (mCustomMenu!=
        
          null
        
        )
			
        
          return
        
         mCustomMenu.dispatchTouchEvent(event,
        
          super
        
        .dispatchTouchEvent(event));
		
        
          return
        
        
          super
        
        .dispatchTouchEvent(event);
	}
	
	
}
      

實現的效果如下:

image image


作者: 藍之風 ?
出處: http://www.cnblogs.com/vaiyanzi/ ?
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。

?

android 自定義菜單


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 成人午夜久久精品 | 亚洲小视频在线 | 欧美午夜场 | 最近更新在线中文字幕一页 | 亚洲欧美成人网 | 国产日产欧美一区二区三区 | 手机看片福利永久国产日韩 | 免费看一级a一片毛片 | 色小妹综合 | 国产在线视精品麻豆 | 久久精品re | 国产欧美日韩精品一区二区三区 | 亚洲综合极品香蕉久久网 | 鲁鲁狠色综合色综合网站 | 久青草国产手机在线观 | 老司机精品在线观看 | 污视频在线看网站 | 免费视频一区二区性色 | 99久久国产综合精麻豆 | 天天干天天爽天天射 | 精品精品国产欧美在线观看 | 久久精品亚洲牛牛影视 | 国产国拍亚洲精品福利 | 男人天堂999| 亚洲精品乱码国产精品乱码 | 奇米影视奇奇米色狠狠色777 | 中文字幕专区高清在线观看 | 中文偷拍视频在线观看 | 国产成人在线观看免费网站 | 中文字幕综合久久久久 | 日韩免费片 | 九九视频在线观看6 | 高清国产天干天干天干不卡顿 | 欧美精彩狠狠色丁香婷婷 | 欧美手机手机在线视频一区 | 激情综合视频 | 伊人俺去久久涩五月综合 | 国产亚洲精品国产第一 | 色片在线 | 人人爽影院 | 手机看片日韩 |