自定义DropDownMenu菜单
在JayFang1993 / DropDownMenu的基础上进行修改,最后的到自己想要的效果
本来的效果:

而我最后实现的效果是:




我们先来分析一下DropDownMenu的源代码

需要改动的地方
1.Menu下面多了一个横线
2.点击变换按钮之后,按钮下面的横线以及字体,还有右边的arrow都变成红色,并且再次点击同一个menu,仍然是红色
解决横线问题
首先在menu_item中增加一个View
<View
android:id="@+id/menu_divider"
android:layout_width="35dp"
android:layout_height="4dp"
android:layout_alignLeft="@id/tv_menu_title"
android:layout_alignRight="@id/tv_menu_title"
android:layout_below="@id/tv_menu_title"
android:layout_centerHorizontal="true"
android:layout_marginTop="4dp"
android:background="@color/divider_menu"
android:visibility="invisible" />
在DropDownMenu修改
增加变量
private boolean mShowMenuDivider;
在init方法中设置mShowMenuDivider = true;
为了当Menu点击选项变化时,改变字体颜色和显示横线,我重写了OnMenuSelectedListener用于监听选择的主Menu改变事件,又增加了一个OnMenuItemSelectedListener监听器,用于监听Menu展开后的点击Item事件
public interface OnMenuSelectedListener {
public void OnMenuChanged(List<ImageView> mIvMenuArrow, View view, int nowRowIndex);
}
本来我没有加mIvMenuArrow这个参数的,后面会讲为什么增加这个参数
在MainActivity中实现OnMenuSelectedListener接口,重写onMenuChanged方法
观察以下代码
通过循环画出menu的选项页面,并调用监听方法
for (int i = 0; i < mMenuCount; i++) {
// final View menuDivider;
final RelativeLayout v = (RelativeLayout) LayoutInflater.from(
mContext).inflate(R.layout.menu_item, null, false);
// menuDivider = v.findViewById(R.id.menu_divider);
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(
width / mMenuCount, LayoutParams.WRAP_CONTENT);
v.setLayoutParams(parms);
TextView tv = (TextView) v.findViewById(R.id.tv_menu_title);
tv.setTextColor(mMenuTitleTextColor);
tv.setTextSize(mMenuTitleTextSize);
if (mDefaultMenuTitle == null || mDefaultMenuTitle.length == 0) {
tv.setText(mMenuItems.get(i)[0]);
} else {
tv.setText(mDefaultMenuTitle[i]);
}
this.addView(v, i);
mTvMenuTitles.add(tv);
RelativeLayout rl = (RelativeLayout) v
.findViewById(R.id.rl_menu_head);
rl.setBackgroundColor(mMenuBackColor);
mRlMenuBacks.add(rl);
ImageView iv = (ImageView) v.findViewById(R.id.iv_menu_arrow);
mIvMenuArrow.add(iv);
mIvMenuArrow.get(i).setImageResource(mArrow.get(i).get("down"));
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) iv
.getLayoutParams();
params.leftMargin = mArrowMarginTitle;
iv.setLayoutParams(params);
final int index = i;
v.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Log.i("tanjin", "DropDwonMenu------->OnClick");
mMenuList.setAdapter(mMenuAdapters.get(index));
if (mMenuAdapters.get(index).getCount() > mShowCount) {
View childView = mMenuAdapters.get(index).getView(
0, null, mMenuList);
childView.measure(MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED));
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, childView
.getMeasuredHeight() * mShowCount);
mMenuList.setLayoutParams(parms);
} else {
View childView = mMenuAdapters.get(index).getView(
0, null, mMenuList);
childView.measure(MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED));
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
mMenuList.setLayoutParams(parms);
}
if (mMenuSelectedListener == null && isDebug)
Toast.makeText(mContext,
"MenuSelectedListener is null",
Toast.LENGTH_LONG).show();
else {
//调用监听方法
mMenuSelectedListener.OnMenuChanged(mIvMenuArrow,
view, index);
}
if (!mShowDivider) {
mMenuList.setDivider(null);
}
mMenuList.setBackgroundColor(mMenuListBackColor);
mColumnSelected = index;
mTvMenuTitles.get(index).setTextColor(
mMenuPressedTitleTextColor);
mRlMenuBacks.get(index).setBackgroundColor(
mMenuPressedBackColor);
mIvMenuArrow.get(index).setImageResource(
mArrow.get(index).get("up"));
mPopupWindow.showAsDropDown(v);
}
});
}
在Activity中设置监听器,并重写OnMenuChanged方法
menu.setMenuSelectedListener(new OnMenuSelectedListener() {
private ArrayList<Map<String, Integer>> mArrow = new ArrayList<>();
@Override
public void OnMenuChanged(List<ImageView> mIvMenuArrow, View view,
int nowRowIndex) {
// TODO Auto-generated method stub
Log.i("tanjin", "MainActivity------>OnMenuChanged");
for (int i = 0; i < menu.getChildCount(); i++) {
View v = menu.getChildAt(i);
v.findViewById(R.id.menu_divider).setVisibility(
View.INVISIBLE);
((TextView) v.findViewById(R.id.tv_menu_title))
.setTextColor(getResources().getColor(
R.color.default_menu_text));
if (i != nowRowIndex) {
mIvMenuArrow.get(i).setImageResource(
R.drawable.arrow_down);
}
}
menu.setmArrow(nowRowIndex);
view.findViewById(R.id.menu_divider)
.setVisibility(View.VISIBLE);
TextView tv = (TextView) view.findViewById(R.id.tv_menu_title);
tv.setTextColor(getResources().getColor(
R.color.default_menu_press_text));
Toast.makeText(CollectionActivity.this, " " + nowRowIndex,
Toast.LENGTH_SHORT).show();
}
});
自定义DropDownMenu菜单的更多相关文章
- 自定义右键菜单,禁用浏览器自带的右键菜单[右键菜单实现--Demo]
许多从事Web开发的会发现有些事,我们需要禁用浏览器本事自带的右键菜单,而实现自定义的右键菜单下面我们也来实现一个自定义的右键菜单 首先来创建JSP页面 <%@ page language=&q ...
- 【连载】Bootstrap开发漂亮的前端界面之自定义右键菜单
连载: 1<教你用Bootstrap开发漂亮的前端界面> 2.<Bootstrap开发漂亮的前端界面之实现原理> 网页中的自定义右键菜单越来越普遍,自定义右键菜单可以增强用户体 ...
- ASP.NET Aries 入门开发教程8:树型列表及自定义右键菜单
前言: 前面几篇重点都在讲普通列表的相关操作. 本篇主要讲树型列表的操作. 框架在设计时,已经把树型列表和普通列表全面统一了操作,用法几乎是一致的. 下面介绍一些差距化的内容: 1:树型列表绑定: v ...
- SharePoint 2013 自定义扩展菜单
在对SharePoint进行开发或者功能扩展的时候,经常需要对一些默认的菜单进行扩展,以使我们开发的东西更适合SharePoint本身的样式.SharePoint的各种功能菜单,像网站设置.Ribbo ...
- SharePoint 2013 自定义扩展菜单(二)
接博文<SharePoint 2013 自定义扩展菜单>,多加了几个例子,方便大家理解. 例七 列表设置菜单扩展(listedit.aspx) 扩展效果 XML描述 <CustomA ...
- JavaScript自定义右键菜单
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- SharePoint开发 - 自定义导航菜单(一)菜单声明与配置
博客地址 http://blog.csdn.net/foxdave 本篇描述自定义sharepoint菜单的一种方式,自定义菜单适用于一些门户等需求的网站 自定义的菜单有自己的数据源,可以是数据表,可 ...
- jquery easyui鼠标右击显示自定义的菜单
1.datagrid表格中,对某一行鼠标右击,显示出如下的自定义的菜单: 在html页面中写: <div id="menu" class="easyui-menu& ...
- 为SharePoint网站创建自定义导航菜单
转:http://kaneboy.blog.51cto.com/1308893/397779 相信不少人都希望把SharePoint网站内置的那个顶部导航菜单,换成自己希望的样式.由于SharePoi ...
随机推荐
- JavaScript中的跨域详解(一)
同源策略 所谓的同源策略,指的是浏览器对不同源的脚本或者文本访问方式进行的限制. 所谓同源,就是指两个页面具有相同的协议,主机(也常说域名),端口,三个要素缺一不可. 同源政策的目的,是为了保证用户信 ...
- mysql注入快速学习基础
前言: sql注入想学好,学通.必须得了解一下基础的SQL 语句.这里我快速理一理 正文: 搭建环境建议下phpsduy快速搭建 select * from kasi select 字段名 from ...
- linux grep日志查询
ll access.2018-09-*.gz zcat access.2018-09-*.gz |grep --color '1073011900' | head -n 100 匹配字符由于管道h ...
- Resetting the Root Password Using rd.break for RHEL7
Start the system and, on the GRUB 2 boot screen, press the e key for edit. Remove the rhgb and quiet ...
- django模版 常用变量
在request中有很多有用的东西,如下: 获取当前用户: 1 {{ request.user }} 如果登陆就显示内容,不登陆就不显示内容: 1 2 3 4 5 {% if request.user ...
- UNITY Destroy()和DestroyImadiate()都不会立即释放对象内存
如题,destroyimadiate是立即将物体从场景hierachy中移除,并标记为 "null",注意 是带引号的null.这是UNITY内部的一个处理技巧.关于这个技巧有很争 ...
- Vulkan Tutorial 26 Image view and sampler
操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 在本章节我们将为图形管线创建另外两个资源来对图像进行采样.第一个资源我们之前已经接触 ...
- CSS隐藏元素 display visibility opacity的区别
{ display: none; /* 不占据空间,无法点击 */ } { visibility: hidden; /* 占据空间,无法点击 */ } { position: absolute; ...
- 【HDU4734】F(x) 【数位dp】
题意 先定义了一个函数F(X)=An*2^n-1+An-1*2^n-2+.....+A1*1.其中Ai为X的第i位的值.对于每组数据给出了两个整数A,B.问不超过B的数中有多少的F值是不超过F(A)的 ...
- 对称二叉树 · symmetric binary tree
[抄题]: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...