注释:此案例主要展示自定义底部菜单,一处封装处处调用。使用起来相当方便

一、初始的Activity

package com.example.myapi.buttommenu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.example.myapi.R;
import com.example.myapi.buttommenu.MenuUtils.MeunOnClickListener;

public class Menu1Activity extends Activity implements MeunOnClickListener,OnClickListener{
    private MenuUtils menuUtils;
    private Button btn_two;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu1);
        menuUtils = (MenuUtils)findViewById(R.id.menu_utils);
        menuUtils.setMenuListener(this);
        btn_two = (Button)findViewById(R.id.btn_two);
        btn_two.setOnClickListener(this);
    }

    @Override
    public void menuOne() {
        Toast.makeText(this, "您点击了菜单一", Toast.LENGTH_LONG).show();
    }

    @Override
    public void menuTwo() {
        Toast.makeText(this, "您点击了菜单二", Toast.LENGTH_LONG).show();
    }

    @Override
    public void menuThree() {
        Toast.makeText(this, "您点击了菜单三", Toast.LENGTH_LONG).show();
    }

    @Override
    public void menuFour() {
        Toast.makeText(this, "您点击了菜单四", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.btn_two:
            Intent intent = new Intent();
            intent.setClass(this, Menu2Activity.class);
            startActivity(intent);
            break;
        }
    }

}

第二个Activity

package com.example.myapi.buttommenu;

import com.example.myapi.R;
import com.example.myapi.buttommenu.MenuUtils.MeunOnClickListener;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class Menu2Activity extends Activity implements MeunOnClickListener{
    private MenuUtils menuUtils;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu2);
        menuUtils = (MenuUtils)findViewById(R.id.menu_utils);
        menuUtils.setMenuListener(this);
    }

    @Override
    public void menuOne() {
        Toast.makeText(this, "您点击了菜单一", Toast.LENGTH_LONG).show();
    }

    @Override
    public void menuTwo() {
        Toast.makeText(this, "您点击了菜单二", Toast.LENGTH_LONG).show();
    }

    @Override
    public void menuThree() {
        Toast.makeText(this, "您点击了菜单三", Toast.LENGTH_LONG).show();
    }

    @Override
    public void menuFour() {
        Toast.makeText(this, "您点击了菜单四", Toast.LENGTH_LONG).show();
    }
}

自定义Menu

package com.example.myapi.buttommenu;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

import com.example.myapi.R;

/**
 * 菜单工具类
 * @author
 *  *
 */
public class MenuUtils extends LinearLayout implements OnClickListener{
    private Button btn_1;
    private Button btn_2;
    private Button btn_3;
    private Button btn_4;
    private Context context;
    private MeunOnClickListener listener;
    public MenuUtils(Context context) {
        super(context);
        this.context = context;
        initView(context);
    }
    public MenuUtils(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }
    private void initView(Context context){
        LinearLayout menuView = (LinearLayout)LayoutInflater.from(context).inflate(com.example.myapi.R.layout.menutuils, null);
        addView(menuView);
        btn_1 = (Button)menuView.findViewById(R.id.btn_1);
        btn_2 = (Button)menuView.findViewById(R.id.btn_2);
        btn_3 = (Button)menuView.findViewById(R.id.btn_3);
        btn_4 = (Button)menuView.findViewById(R.id.btn_4);
        btn_1.setOnClickListener(this);
        btn_2.setOnClickListener(this);
        btn_3.setOnClickListener(this);
        btn_4.setOnClickListener(this);
    }
    public void setMenuListener(MeunOnClickListener listener){
        this.listener = listener;
    }
    public interface MeunOnClickListener{
        public void menuOne();
        public void menuTwo();
        public void menuThree();
        public void menuFour();
    }
    @Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.btn_1:
            listener.menuOne();
            break;
        case R.id.btn_2:
            listener.menuTwo();
            break;
        case R.id.btn_3:
            listener.menuThree();
            break;
        case R.id.btn_4:
            listener.menuFour();
            break;
        }
    }

}

一下是布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
       <com.example.myapi.buttommenu.MenuUtils
        android:id="@+id/menu_utils"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        />
    <Button
        android:id="@+id/btn_two"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="跳转到第二个界面"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <com.example.myapi.buttommenu.MenuUtils
        android:id="@+id/menu_utils"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="菜单一"/>
        <Button
            android:id="@+id/btn_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="1"
            android:text="菜单二"/>
        <Button
            android:id="@+id/btn_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="1"
            android:text="菜单三"/>
        <Button
            android:id="@+id/btn_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="1"
            android:text="菜单四"/>
    </LinearLayout>

</LinearLayout>

完毕

Android 自定义底部公用菜单的更多相关文章

  1. Android自定义底部带有动画的Dialog

    Android自定义底部带有动画的Dialog 效果图 先看效果图,是不是你想要的呢 自定义Dialog package --.view; import android.app.Dialog; imp ...

  2. android 自定义下拉菜单

    本实例的自定义下拉菜单主要是继承PopupWindow类来实现的弹出窗体,各种布局效果可以根据自己定义设计.弹出的动画效果主要用到了translate.alpha.scale,具体实现步骤如下: 先上 ...

  3. Android 自定义View修炼-Android 实现自定义的卫星式菜单(弧形菜单)View

    一.总述 Android 实现卫星式菜单也叫弧形菜单的主要要做的工作如下:1.动画的处理2.自定义ViewGroup来实现卫星式菜单View (1)自定义属性       a. 在attrs.xml中 ...

  4. Android 自定义ListView实现底部分页刷新与顶部下拉刷新,androidlistview

    在项目开发中,由于数据过大时,需要进行分页加载或下拉刷新,来缓解一次性加载的过长等待.本篇博文实例讲解通过自定义的ListView实现底部分页加载和顶部下拉刷新的效果. 其效果图: 一.ListVie ...

  5. Android自定义顶部栏及侧滑菜单和fragment+viewpag滑动切换的实现

    嘿嘿嘿,关于android滑动的操作,是不是经常都会用到呢. 我肯定也要学习一下啦. https://blog.csdn.net/u013184970/article/details/82882107 ...

  6. Android 自定义View合集

    自定义控件学习 https://github.com/GcsSloop/AndroidNote/tree/master/CustomView 小良自定义控件合集 https://github.com/ ...

  7. Xamarin.Android之ActionBar与菜单

    一.选项卡 如今很多应用都会使用碎片以便在同一个活动中能够显示多个不同的视图.在Android 3.0 以上的版本中,我们已经可以使用ActionBar提供的Tab来实现这种效果,而不需要我们自己去实 ...

  8. Android自定义View4——统计图View

    1.介绍 周末在逛慕课网的时候,看到了一张学习计划报告图,详细记录了自己一周的学习情况,天天都是0节课啊!正好在学习Android自定义View,于是就想着自己去写了一个,这里先给出一张慕课网的图,和 ...

  9. Android 自定义View (五)——实践

    前言: 前面已经介绍了<Android 自定义 view(四)-- onMeasure 方法理解>,那么这次我们就来小实践下吧 任务: 公司现有两个任务需要我完成 (1)监测液化天然气液压 ...

随机推荐

  1. MVC 、JDBC、SQL、DBMS、RDBMS、DDL、DML、DCL

    MVC: 全称:Model View Controller: 解释:模型(model)-视图(view)-控制器(controller) Model(模型)表示应用程序核心(比如数据库记录列表). V ...

  2. js-redux学习笔记2

    1.为了 dispatch 一个 action,我们需要一个 dispatch 函数.可以用一个 action creator 去发送一个 action. var setNameActionCreat ...

  3. Flask结合Redis消息队列实现电影弹幕

    用到的弹幕播放器插件:dplayer.js(开源) 1.安装Redis 2.安装flask-redis包 pip install flask-redis3.下载dplayer 页面搭建 1.引入资源 ...

  4. ActiveReports 报表控件V12新特性 -- 新增矩表的RepeatToFill属性

    ActiveReports是一款专注于 .NET 平台的报表控件,全面满足 HTML5 / WinForms / ASP.NET / ASP.NET MVC / WPF 等平台下报表设计和开发工作需求 ...

  5. SQLServer 学习笔记之超详细基础SQL语句 Part 5

    Sqlserver 学习笔记 by:授客 QQ:1033553122 -----------------------接Part 4------------------- 21使用默认 默认(也称默认值 ...

  6. WOSA/XFS PTR Form解析库—头文件

    class AFX_EX_CLASS CNuXfsForm {public: CNuXfsForm(); ~CNuXfsForm(); /******************************* ...

  7. AppManager

    1.统一应用程序中所有的Activity的栈管理  涉及到activity的添加.删除指定.删除当前.删除所有.返回栈大小的方法 public class AppManager { private S ...

  8. 【Redis】Redis学习(二) master/slave、sentinel、Cluster简单总结

    项目中用到Redis,所以准备学习一下,感觉Redis的概念还是很多的,什么主从模式.sentinel模式.集群模式的,一下子都晕了,我觉得还是有必要先理清这些基本概念再说. 一.单节点实例 单节点实 ...

  9. Android 借助Stetho在Chrome上调试Android网络、数据库、Sharedpreferences

    Android 借助Stetho在Chrome上调试Android网络.数据库.Sharedpreferences 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/a ...

  10. CSS3动画详解(图文教程)

    本文最初发表于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 以下是正文. 前言 本文主要内容: 过渡:transition 2D 转换 trans ...