最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android編程自定義菜單實(shí)現(xiàn)方法詳解

 更新時(shí)間:2017年02月22日 14:23:01   作者:藍(lán)之風(fēng)  
這篇文章主要介紹了Android編程自定義菜單實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android自定義菜單的布局、動(dòng)畫(huà)及功能相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了Android編程自定義菜單實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:

在android開(kāi)發(fā)的過(guò)程中系統(tǒng)自帶的菜單往往滿(mǎn)足不了開(kāi)發(fā)中的一些需求,比如說(shuō)一排最多只能放置三個(gè)菜單,坐多只能放置6個(gè),再多的話(huà)就會(huì)折疊起來(lái),如果我們想再一排顯示4個(gè)或5個(gè)菜單那么就要自己想辦法處理。

這里我用布局的隱藏并加上動(dòng)畫(huà)來(lái)模擬菜單的效果。

要點(diǎn):

1、隱藏和顯示菜單,我使用了一個(gè)線(xiàn)性布局把菜單封裝起來(lái)。

<?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、模擬菜單的效果,增加動(dòng)畫(huà),布局顯示的時(shí)候增加一個(gè)漸漸底部生氣的效果,隱藏的時(shí)候增加一個(gè)緩緩下落的效果,顯示菜單動(dòng)畫(huà)文件:

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

隱藏菜單動(dòng)畫(huà)文件:

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

動(dòng)畫(huà)調(diào)用:

 /**
  * 顯示菜單欄, 重新實(shí)現(xiàn)的Option menu.
  * */
 private void showAppMenu() {
   if (menuShowAnimation == null) {
     menuShowAnimation = AnimationUtils
         .loadAnimation(mContext, R.anim.menuhide);
   }
   myLayout.startAnimation(menuShowAnimation);
   myLayout.setVisibility(View.VISIBLE);
 }
 /**
  * 隱藏菜單欄, 重新實(shí)現(xiàn)的Option menu.
  * */
 private void hideAppMenu() {
   myLayout.setVisibility(View.GONE);
   if (menuHideAnimation == null)
     menuHideAnimation =AnimationUtils
         .loadAnimation(mContext, R.anim.menushow);
   myLayout.startAnimation(menuHideAnimation);
 }

3、控制菜單的隱藏和顯示,需要重寫(xiě)三個(gè)方法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、實(shí)現(xiàn)菜單點(diǎn)擊時(shí)候被點(diǎn)擊菜單狀態(tài)的般變化,這里我使用了selector來(lái)實(shí)現(xiàn),菜單我使用ImageButton將selector賦值給ImageButton 的background即可:

一個(gè)菜單項(xiàng)

<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 文件內(nèi)容如下:

<?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、頁(yè)面的調(diào)用使用:<include>標(biāo)簽來(lái)進(jìn)行引用:

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

這樣的話(huà)一個(gè)模擬的自定義菜單就基本完成了,菜單控制完整代碼java類(lèi):

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: 創(chuàng)建時(shí)間:2011-8-30 上午11:16:19
 * 說(shuō)   明:
 * 修改歷史:
 */
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);
    //返回首頁(yè)
    imgIndex.setOnClickListener(new OnClickListener(){
      public void onClick(View v) {
        //TODO do somthing
      }
    });
    //設(shè)置
    imgSet.setOnClickListener(new OnClickListener(){
      public void onClick(View v) {
        //TODO do somthing
      }
    });
    //查詢(xún)
    imgNews.setOnClickListener(new OnClickListener(){
      public void onClick(View v) {
        //TODO do somthing
      }
    });
    //編輯
    imgAdd.setOnClickListener(new OnClickListener(){
      public void onClick(View v) {
        //TODO do somthing
      }
    });
    //退出系統(tǒng)
    imgQuit.setOnClickListener(new OnClickListener(){
      public void onClick(View v) {
        //TODO do somthing
      }
    });
    //素材庫(kù)
    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);
  }
   /**
  * 顯示菜單欄, 重新實(shí)現(xiàn)的Option menu.
  * */
 private void showAppMenu() {
   if (menuShowAnimation == null) {
     menuShowAnimation = AnimationUtils
         .loadAnimation(mContext, R.anim.menuhide);
   }
   myLayout.startAnimation(menuShowAnimation);
   myLayout.setVisibility(View.VISIBLE);
 }
 /**
  * 隱藏菜單欄, 重新實(shí)現(xiàn)的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調(diào)用菜單完整代碼:

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: 創(chuàng)建時(shí)間:2011-8-30 上午11:13:14
 * 說(shuō)   明:
 * 修改歷史:
 */
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);
  }
}

實(shí)現(xiàn)的效果如下:

更多關(guān)于A(yíng)ndroid相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android布局layout技巧總結(jié)》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Android ListView之setEmptyView正確使用方法

    Android ListView之setEmptyView正確使用方法

    這篇文章主要介紹了Android ListView之setEmptyView正確使用方法的相關(guān)資料,希望通過(guò)本文能幫助到大家使用該方法,需要的朋友可以參考下
    2017-09-09
  • 利用Jetpack Compose實(shí)現(xiàn)經(jīng)典俄羅斯方塊游戲

    利用Jetpack Compose實(shí)現(xiàn)經(jīng)典俄羅斯方塊游戲

    你的童年是否有俄羅斯方塊呢,本文就來(lái)介紹如何通過(guò)Jetpack Compose實(shí)現(xiàn)一個(gè)俄羅斯方塊!感興趣的小伙伴快跟隨小編一起動(dòng)手嘗試一下吧
    2022-05-05
  • Android Gradle多渠道打包的實(shí)現(xiàn)方法

    Android Gradle多渠道打包的實(shí)現(xiàn)方法

    這篇文章主要介紹了Android Gradle多渠道打包的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Android SQLite數(shù)據(jù)庫(kù)連接實(shí)現(xiàn)登錄功能

    Android SQLite數(shù)據(jù)庫(kù)連接實(shí)現(xiàn)登錄功能

    這篇文章主要為大家詳細(xì)介紹了Android SQLite數(shù)據(jù)庫(kù)連接實(shí)現(xiàn)登錄功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • Android仿抖音上下滑動(dòng)布局

    Android仿抖音上下滑動(dòng)布局

    這篇文章主要為大家詳細(xì)介紹了Android仿抖音上下滑動(dòng)布局,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • Android通過(guò)手勢(shì)實(shí)現(xiàn)答題器翻頁(yè)效果

    Android通過(guò)手勢(shì)實(shí)現(xiàn)答題器翻頁(yè)效果

    這篇文章主要為大家詳細(xì)介紹了Android通過(guò)手勢(shì)實(shí)現(xiàn)答題器翻頁(yè)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Android ListView自定義Adapter實(shí)現(xiàn)仿QQ界面

    Android ListView自定義Adapter實(shí)現(xiàn)仿QQ界面

    這篇文章主要為大家詳細(xì)介紹了ListView自定義Adapter實(shí)現(xiàn)仿QQ界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Android?Retrofit使用詳細(xì)教程

    Android?Retrofit使用詳細(xì)教程

    Retrofit是Android用來(lái)接口請(qǐng)求的網(wǎng)絡(luò)框架,內(nèi)部是基于OkHttp實(shí)現(xiàn)的,retrofit負(fù)責(zé)接口請(qǐng)求的封裝,retrofit可以直接將接口數(shù)據(jù)解析為Bean類(lèi)、List集合等,直接簡(jiǎn)化了中間繁瑣的數(shù)據(jù)解析過(guò)程,這篇文章主要介紹了Android?Retrofit使用詳情,需要的朋友可以參考下
    2024-03-03
  • android頂部(toolbar)搜索框?qū)崿F(xiàn)代碼

    android頂部(toolbar)搜索框?qū)崿F(xiàn)代碼

    這篇文章主要介紹了android頂部(toolbar)搜索框?qū)崿F(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • Flutter學(xué)習(xí)之構(gòu)建、布局及繪制三部曲

    Flutter學(xué)習(xí)之構(gòu)建、布局及繪制三部曲

    這篇文章主要給大家介紹了關(guān)于Flutter學(xué)習(xí)之構(gòu)建、布局及繪制三部曲的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04

最新評(píng)論

杨浦区| 廊坊市| 洛川县| 阿尔山市| 南陵县| 汾西县| 图木舒克市| 怀仁县| 临猗县| 高碑店市| 如东县| 荆州市| 阳朔县| 阳城县| 普格县| 雷山县| 克山县| 宾阳县| 漾濞| 昌江| 沙洋县| 峡江县| 墨脱县| 紫金县| 嵊州市| 老河口市| 平度市| 玉门市| 沭阳县| 沾益县| 汨罗市| 平安县| 巩义市| 游戏| 永康市| 家居| 中牟县| 剑阁县| 眉山市| 左云县| 蕲春县|