android 通用菜单条实现(一)
一、前言介绍
直奔主题啦,非常多Android app都有菜单条。菜单条除了背景图片、图标的不同外,布局基本一致。大致能够分为三部分:菜单条的左側区域、菜单条中间区域、菜单条右側区域。
为了考虑代码的重用性,本文将给大家解说通用菜单条的实现方式。演示样例中的代码。大家略微变通。能够满足大部分软件开发须要。
二、演示样例截图
我的一贯习惯,有图有真相。以下先看下通用菜单条的截图:
三、实现介绍
3.1菜单条布局文件:title_top_view.xml
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
> <RelativeLayout android:id="@+id/title_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/title_bg"> <!-- 左側区域 -->
<ImageButton android:id="@+id/left_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dip"
android:background="@drawable/select_back"/> <!-- 中间区域 -->
<TextView android:id="@+id/mid_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:singleLine="true"
android:ellipsize="end"
android:layout_marginLeft="60dip"
android:layout_marginRight="60dip"
/> <!-- 右側区域 -->
<ImageButton android:id="@+id/right_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dip"
android:layout_centerVertical="true"
android:background="@drawable/selector_setting"/>
</RelativeLayout> </RelativeLayout>
</span>
3.2 MainActivity页面布局文件:activity_main.xml
<span style="font-size:14px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
> <!-- 通过该标签导入菜单条 -->
<include
android:id="@+id/title_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/title_top_view"/> <TextView
android:layout_below="@id/title_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> </RelativeLayout>
</span>
3.3java代码部分
提到java代码部分,先看通用菜单条代码设计类图,例如以下:
类图说明:本Demo将菜单条的左側区域(mLeftView)、中间区域(mMidView)、右側区域(mRightView)成员声明为protected,有违反代码封装性,各位能够下载Demo自行改动为private,并提供对外接口。本Demo主要用意方便子类訪问、提供訪问速度。
BaseActivity.java 代码例如以下:
<span style="font-size:14px;">package com.example.titledemo; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast; public abstract class BaseActivity extends Activity implements OnClickListener { protected View mTitleView;
protected ImageView mLeftView;// 左側button
protected TextView mMidView;// 中间文本
protected ImageView mRightView;// 右側button @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState); // 设置标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE); initView(savedInstanceState);
} @Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.left_btn: {
onClickLeftBtn();
break;
}
case R.id.right_btn: {
onClickRigthBtn();
break;
}
default: {
break;
}
}
} /**
* 初始化菜单条
*/
protected void initTitleBar() {
mTitleView = findViewById(R.id.title_bar);
if (mTitleView != null) {
mTitleView.setVisibility(View.VISIBLE);
mLeftView = (ImageView) findViewById(R.id.left_btn);
mLeftView.setOnClickListener(this);
mMidView = (TextView) findViewById(R.id.mid_txt);
mRightView = (ImageView) findViewById(R.id.right_btn);
mRightView.setOnClickListener(this);
}
} /**
* 设置中间文本
*/
protected void setMidTxt(String strTxt) {
if (mMidView != null) {
mMidView.setText(strTxt);
}
} /**
* 初始化页面
* @param savedInstanceState
*/
protected abstract void initView(Bundle savedInstanceState); /**
* 单击菜单条左側button。响应处理函数,子类可继承实现自己的处理方式
*/
protected abstract void onClickLeftBtn();
protected abstract void onClickRigthBtn();
}
</span>
MainActivity.java 代码例如以下:
<span style="font-size:14px;">package com.example.titledemo; import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Toast; public class MainActivity extends BaseActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
} @Override
protected void initView(Bundle savedInstanceState) {
// TODO Auto-generated method stub setContentView(R.layout.activity_main); //设置菜单条
initTitleBar(); //设置菜单中间文本值
setMidTxt(getResources().getString(R.string.app_name));
} @Override
protected void onClickLeftBtn() {
// TODO Auto-generated method stub
Toast.makeText(this, "点击了菜单左側button", Toast.LENGTH_SHORT).show();
} @Override
protected void onClickRigthBtn() {
// TODO Auto-generated method stub
Toast.makeText(this, "点击了菜单右側button", Toast.LENGTH_SHORT).show();
} }
</span>
四、演示样例下载
下面为Demo演示样例代码下载路径,http://download.csdn.net/detail/improveyourself/7505935
ps:假设各位有更好的实现方式。能够给我留言,在此先感谢各位。
android 通用菜单条实现(一)的更多相关文章
- android 三级菜单 BaseExpandableListAdapter
在网上搜了非常长时间.没有找到合适的Android三级菜单.所以就自己动手写了一个,主要使用了BaseExpandableList来实现,通过三个布局文件来完毕相应的菜单项,详细实现请參照下图. wa ...
- 15类Android通用流行框架
15类Android通用流行框架 Android流行框架 缓存 DiskLruCache Java实现基于LRU的磁盘缓存 图片加载 Android Universal Image Loader 一个 ...
- Redrain 通用菜单控件使用方法和说明(附源码和demo)
转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/42889709 大概半年前我写过博客说明怎么改造duilib的原代Menu ...
- Android抽屉菜单DrawerLayout的实现案例
(1)项目布局文件 activity_main.xml <android.support.v4.widget.DrawerLayout xmlns:android="http://sc ...
- Android侧滑菜单代码实现
前两天学习了hyman老师讲的Android侧滑菜单的实现,经过自己的整理分享出来给大家学习一下 现在很多APP都有菜单侧滑的功能,本篇文章主要讲解使用自定义的HorizontalScrollView ...
- 60.Android通用流行框架大全
转载:https://segmentfault.com/a/1190000005073746 Android通用流行框架大全 1. 缓存 名称 描述 DiskLruCache Java实现基于LRU的 ...
- Android开发60条技术经验总结
Android开发60条技术经验总结,以下是全文: 1. 全部Activity可继承自BaseActivity,便于统一风格与处理公共事件,构建对话框统一构建器的建立,万一需要整体变动,一处修改到处有 ...
- Android 设置进度条背景
Android 设置进度条背景 直接上代码 <ProgressBar android:id="@+id/progressBar" android:layout_width=& ...
- android 自定义进度条颜色
android 自定义进度条颜色 先看图 基于产品经理各种自定义需求,经过查阅了解,下面是自己对Android自定义进度条的学习过程! 这个没法了只能看源码了,还好下载了源码, sources\b ...
随机推荐
- [转]自适应网页设计(Responsive Web Design)
本文转自:http://www.ruanyifeng.com/blog/2012/05/responsive_web_design.html 作者: 阮一峰 日期: 2012年5月 1日 随着3G的普 ...
- conda python虚拟环境
#查看已安装的python包 conda list #查看当前有哪些虚拟环境 conda env list 或者 conda info -e #更新conda conda update conda # ...
- Spring boot中的定时任务(计划任务)
从Spring3.1开始,计划任务在Spring中实现变得异常的简单.首先通过配置类注解@EnableScheduling来开启对计划任务的支持,然后再要执行的计划任务的方法上注释@Scheduled ...
- android中TextView内容竖向显示
项目中遇到需要textview内容竖着排的需求,如图所示: 网上那些“教程”并不能达到需要的效果,发现有一个属性可以支持这种效果,android:ems=“*”,这是属性表示一行只显示*个字符. 具体 ...
- 微信自定义分享功能实现Tips
以MVC为例 前台js通过.post()方法传给后台特定Controller当前页面的url,后台获取后,进行处理: 1.获取access_token:https://mp.weixin.qq.com ...
- linux 卸载 mongo2.6
要求:linux 卸载 mongo2.6 版本:linux系统:Ubuntu 16.04 mongo: mongo 2.6.12 1. 查看安装的mongo版本和服务 # dpkg –l | gr ...
- Angular——tab切换案例
基本介绍 angular框架下的tab切换,相比较于之前的纯js写的代码,有一个很大的特点就是以数据为驱动,基本上不用搜索dom元素就可以实现效果 基本使用 (1)导航部分使用的是的状态使用的是ng- ...
- html5——多媒体(四)
全屏兼容 box.requestFullscreen(); box.webkitRequestFullScreen(); box.mozRequestFullScreen(); <!DOCTYP ...
- Ubuntu 常用解压与压缩命令
参考 https://blog.csdn.net/songbinxu/article/details/80435665 示例: 把/home/wangju/gitlab/automationTest目 ...
- SpringMVC与MyBatis整合方法
一.springmvc+mybaits的系统架构: 第一步:整合dao层 mybatis和spring整合,通过spring管理mapper接口. 使用mapper的扫描器自动扫描mapper接口在s ...