Logo



安卓分类下拉菜单源码(安卓下拉菜单软件)

本文目录一览:

spinner 下拉菜单 安卓源代码怎么注册用户

Spinner控件也是一种列表类型的控件,它的继承关系如下:

Java.lang.Object

? Android.view.View

? android.view.ViewGroup

? android.widget.AdapterViewTextends android.widget.Adapter

? android.widget.AbsSpinner

? android.widget.Spinner

android.widget.Spinner继承了android.view.ViewGroup类。

在Android的UI开发中,Spinner(下拉列表)总是可以用到的,一个简单的自定义Spinner制作我们只需要记住这重要的五步,一个Spinner就可以应用而生了。

求高手帮忙修改下拉菜单源码

%if B_BG1 then%

SCRIPT language=javascript

// 菜单开始

mtDropDown.spacerGif = "images/Menu_x.gif";

mtDropDown.dingbatOn = "images/Menu_expand-1.gif";

mtDropDown.dingbatOff = "images/Menu_expand-0.gif";

mtDropDown.dingbatSize = 14;

mtDropDown.menuPadding = 1;

mtDropDown.itemPadding = 4;

mtDropDown.shadowSize = 2;

mtDropDown.shadowOffset = 3;

mtDropDown.shadowColor = "#888";

mtDropDown.shadowPng = "images/005.png";

mtDropDown.backgroundColor = "#EAEAF4";

mtDropDown.backgroundPng = "images/004.png";

mtDropDown.hideDelay = 200;

mtDropDown.slideTime = 200;

mtDropDown.reference = {topLeft:1,topRight:2,bottomLeft:3,bottomRight:4};

mtDropDown.direction = {down:1,right:2};

mtDropDown.registry = [];

mtDropDown._maxZ = 100;

mtDropDown.isSupported = function() {

if (typeof mtDropDown.isSupported.r == "boolean")

return mtDropDown.isSupported.r;

var ua = navigator.userAgent.toLowerCase();

var an = navigator.appName;

var r = false;

if (ua.indexOf("gecko") -1) r = true;

else if (an == "Microsoft Internet Explorer") {

if (document.getElementById) r = true;

mtDropDown.isSupported.r = r;

return r;

mtDropDown.initialize = function() {

for (var i = 0, menu = null; menu = this.registry[i]; i++) {

menu.initialize();

mtDropDown.renderAll = function() {

var aMenuHtml = [];

for (var i = 0, menu = null; menu = this.registry[i]; i++) {

aMenuHtml[i] = menu.toString();

document.write(aMenuHtml.join(""));

function mtDropDown(oActuator, iDirection, iLeft, iTop, iReferencePoint, parentMenuSet) {

this.addItem = addItem;

this.addMenu = addMenu;

this.toString = toString;

this.initialize = initialize;

this.isOpen = false;

this.show = show;

this.hide = hide;

this.items = [];

this.onactivate = new Function();

this.ondeactivate = new Function();

this.onmouseover = new Function();

this.onqueue = new Function();

this.index = mtDropDown.registry.length;

mtDropDown.registry[this.index] = this;

var id = "mtDropDown" + this.index;

var contentHeight = null;

var contentWidth = null;

var childMenuSet = null;

var animating = false;

var childMenus = [];

var slideAccel = -1;

var elmCache = null;

var ready = false;

var _this = this;

var a = null;

var pos = iDirection == mtDropDown.direction.down ? "top" : "left";

var dim = null;

function addItem(sText, sUrl) {

var item = new mtDropDownItem(sText, sUrl, this);

item._index = this.items.length;

this.items[item._index] = item;

function addMenu(oMenuItem) {

if (!oMenuItem.parentMenu == this) throw new Error("Cannot add a menu here");

if (childMenuSet == null) childMenuSet = new mtDropDownSet(mtDropDown.direction.right, -5, 2, mtDropDown.reference.topRight);

var m = childMenuSet.addMenu(oMenuItem);

childMenus[oMenuItem._index] = m;

m.onmouseover = child_mouseover;

m.ondeactivate = child_deactivate;

m.onqueue = child_queue;

return m;

function initialize() {

initCache();

initEvents();

initSize();

ready = true;

function show() {

if (ready) {

_this.isOpen = true;

animating = true;

setContainerPos();

elmCache["clip"].style.visibility = "visible";

elmCache["clip"].style.zIndex = mtDropDown._maxZ++;

slideStart();

_this.onactivate();

function hide() {

if (ready) {

_this.isOpen = false;

animating = true;

for (var i = 0, item = null; item = elmCache.item[i]; i++)

dehighlight(item);

if (childMenuSet) childMenuSet.hide();

slideStart();

_this.ondeactivate();

function setContainerPos() {

var sub = oActuator.constructor == mtDropDownItem;

var act = sub ? oActuator.parentMenu.elmCache["item"][oActuator._index] : oActuator;

var el = act;

var x = 0;

var y = 0;

var minX = 0;

var maxX = (window.innerWidth ? window.innerWidth : document.body.clientWidth) - parseInt(elmCache["clip"].style.width);

var minY = 0;

var maxY = (window.innerHeight ? window.innerHeight : document.body.clientHeight) - parseInt(elmCache["clip"].style.height);

while (sub ? el.parentNode.className.indexOf("mtDropdownMenu") == -1 : el.offsetParent) {

x += el.offsetLeft;

y += el.offsetTop;

if (el.scrollLeft) x -= el.scrollLeft;

if (el.scrollTop) y -= el.scrollTop;

el = el.offsetParent;

if (oActuator.constructor == mtDropDownItem) {

x += parseInt(el.parentNode.style.left);

y += parseInt(el.parentNode.style.top);

switch (iReferencePoint) {

case mtDropDown.reference.topLeft:

break;

case mtDropDown.reference.topRight:

x += act.offsetWidth;

break;

case mtDropDown.reference.bottomLeft:

y += act.offsetHeight;

break;

case mtDropDown.reference.bottomRight:

x += act.offsetWidth;

y += act.offsetHeight;

break;

x += iLeft;

y += iTop;

x = Math.max(Math.min(x, maxX), minX);

y = Math.max(Math.min(y, maxY), minY);

elmCache["clip"].style.left = x + "px";

elmCache["clip"].style.top = y + "px";

function slideStart() {

var x0 = parseInt(elmCache["content"].style[pos]);

var x1 = _this.isOpen ? 0 : -dim;

if (a != null) a.stop();

a = new Accelimation(x0, x1, mtDropDown.slideTime, slideAccel);

a.onframe = slideFrame;

a.onend = slideEnd;

a.start();

function slideFrame(x) {

elmCache["content"].style[pos] = x + "px";

function slideEnd() {

if (!_this.isOpen) elmCache["clip"].style.visibility = "hidden";

animating = false;

function initSize() {

var ow = elmCache["items"].offsetWidth;

var oh = elmCache["items"].offsetHeight;

var ua = navigator.userAgent.toLowerCase();

elmCache["clip"].style.width = ow + mtDropDown.shadowSize + 2 + "px";

elmCache["clip"].style.height = oh + mtDropDown.shadowSize + 2 + "px";

elmCache["content"].style.width = ow + mtDropDown.shadowSize + "px";

elmCache["content"].style.height = oh + mtDropDown.shadowSize + "px";

contentHeight = oh + mtDropDown.shadowSize;

contentWidth = ow + mtDropDown.shadowSize;

dim = iDirection == mtDropDown.direction.down ? contentHeight : contentWidth;

elmCache["content"].style[pos] = -dim - mtDropDown.shadowSize + "px";

elmCache["clip"].style.visibility = "hidden";

if (ua.indexOf("mac") == -1 || ua.indexOf("gecko") -1) {

elmCache["background"].style.width = ow + "px";

elmCache["background"].style.height = oh + "px";

elmCache["background"].style.backgroundColor = mtDropDown.backgroundColor;

elmCache["shadowRight"].style.left = ow + "px";

elmCache["shadowRight"].style.height = oh - (mtDropDown.shadowOffset - mtDropDown.shadowSize) + "px";

elmCache["shadowRight"].style.backgroundColor = mtDropDown.shadowColor;

elmCache["shadowBottom"].style.top = oh + "px";

elmCache["shadowBottom"].style.width = ow - mtDropDown.shadowOffset + "px";

elmCache["shadowBottom"].style.backgroundColor = mtDropDown.shadowColor;

else {

elmCache["background"].firstChild.src = mtDropDown.backgroundPng;

elmCache["background"].firstChild.width = ow;

elmCache["background"].firstChild.height = oh;

elmCache["shadowRight"].firstChild.src = mtDropDown.shadowPng;

elmCache["shadowRight"].style.left = ow + "px";

elmCache["shadowRight"].firstChild.width = mtDropDown.shadowSize;

elmCache["shadowRight"].firstChild.height = oh - (mtDropDown.shadowOffset - mtDropDown.shadowSize);

elmCache["shadowBottom"].firstChild.src = mtDropDown.shadowPng;

elmCache["shadowBottom"].style.top = oh + "px";

elmCache["shadowBottom"].firstChild.height = mtDropDown.shadowSize;

elmCache["shadowBottom"].firstChild.width = ow - mtDropDown.shadowOffset;

function initCache() {

var menu = document.getElementById(id);

var all = menu.all ? menu.all : menu.getElementsByTagName("*");

elmCache = {};

elmCache["clip"] = menu;

elmCache["item"] = [];

for (var i = 0, elm = null; elm = all[i]; i++) {

switch (elm.className) {

case "items":

case "content":

case "background":

case "shadowRight":

case "shadowBottom":

elmCache[elm.className] = elm;

break;

case "item":

elm._index = elmCache["item"].length;

elmCache["item"][elm._index] = elm;

break;

_this.elmCache = elmCache;

function initEvents() {

for (var i = 0, item = null; item = elmCache.item[i]; i++) {

item.onmouseover = item_mouseover;

item.onmouseout = item_mouseout;

item.onclick = item_click;

if (typeof oActuator.tagName != "undefined") {

oActuator.onmouseover = actuator_mouseover;

oActuator.onmouseout = actuator_mouseout;

如何打造Android自定义的下拉列表框控件

一、概述

Android中的有个原生的下拉列表控件Spinner,但是这个控件有时候不符合我们自己的要求,

比如有时候我们需要类似windows 或者web网页中常见的那种下拉列表控件,类似下图这样的:

这个时候只有自己动手写一个了。其实实现起来不算很难,

本文实现的方案是采用TextView +ImageView+PopupWindow的组合方案。

先来看看我们的自己写的控件效果图吧:(源码在文章下面最后给出哈!)

二、自定义下拉列表框控件的实现

1. 自定义控件用到的布局文件和资源:

结果框的布局页面:dropdownlist_view.xml:

?xml version="1.0" encoding="utf-8"?

RelativeLayout xmlns:android=""

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal"

android:id="@+id/compound"

android:background="@drawable/dropdown_bg_selector"

TextView

android:id="@+id/text"

android:layout_width="250dp"

android:layout_height="40dp"

android:paddingLeft="10dp"

android:text="文本文字"

android:gravity="center_vertical"

android:textSize="14sp"

android:padding="5dp"

android:singleLine="true" /

ImageView

android:id="@+id/btn"

android:layout_width="30dp"

android:layout_height="30dp"

android:layout_toRightOf="@+id/text"

android:src="@drawable/dropdown"

android:padding="5dp"

android:layout_centerVertical="true"

android:gravity="center"/

/RelativeLayout

下拉弹窗列表布局页面:dropdownlist_popupwindow.xml:

?xml version="1.0" encoding="utf-8"?

LinearLayout xmlns:android=""

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

ListView

android:id="@+id/listView"

android:layout_width="280dp"

android:layout_height="wrap_content"

android:divider="#666666"

android:dividerHeight="1dp"

/ListView

/LinearLayout

selector资源文件:

dropdown_list_selector.xml:

?xml version="1.0" encoding="utf-8"?

selector xmlns:android=""

item android:state_pressed="true" android:drawable="@color/dropdownlist_item_press"/

item android:drawable="@color/dropdownlist_item"/

/selector

dropdown_bg_selector.xml:

?xml version="1.0" encoding="utf-8"?

selector xmlns:android=""

item android:state_pressed="true" android:drawable="@color/dropdownlist_press"/

item android:drawable="@color/dropdownlist_bg"/

/selector

2. 自定义下拉列表框控件类的实现:

我们采用了TextView+ImageView+PopupWindow的组合方案,所以我的自定义控件需要重写ViewGroup,由于我们已经知道了,布局方向为竖直方向,所以这里,

我直接继承LinearLayout来写这个控件。具体实现代码如下:

package com.czm.xcdropdownlistview;

import java.util.ArrayList;

import android.annotation.SuppressLint;

import android.content.Context;

import android.util.AttributeSet;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.ListView;

import android.widget.PopupWindow;

import android.widget.TextView;

@SuppressLint("NewApi")

* 下拉列表框控件

* @author caizhiming

public class XCDropDownListView extends LinearLayout{

private TextView editText;

private ImageView imageView;

private PopupWindow popupWindow = null;

private ArrayListString dataList = new ArrayListString();

private View mView;

public XCDropDownListView(Context context) {

this(context,null);

// TODO Auto-generated constructor stub

public XCDropDownListView(Context context, AttributeSet attrs) {

this(context, attrs,0);

// TODO Auto-generated constructor stub

public XCDropDownListView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

// TODO Auto-generated constructor stub

initView();

public void initView(){

String infServie = Context.LAYOUT_INFLATER_SERVICE;

LayoutInflater layoutInflater;

layoutInflater = (LayoutInflater) getContext().getSystemService(infServie);

View view = layoutInflater.inflate(R.layout.dropdownlist_view, this,true);

editText= (TextView)findViewById(R.id.text);

imageView = (ImageView)findViewById(R.id.btn);

this.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

if(popupWindow == null ){

showPopWindow();

}else{

closePopWindow();

* 打开下拉列表弹窗

private void showPopWindow() {

// 加载popupWindow的布局文件

String infServie = Context.LAYOUT_INFLATER_SERVICE;

LayoutInflater layoutInflater;

layoutInflater = (LayoutInflater) getContext().getSystemService(infServie);

View contentView = layoutInflater.inflate(R.layout.dropdownlist_popupwindow, null,false);

ListView listView = (ListView)contentView.findViewById(R.id.listView);

listView.setAdapter(new XCDropDownListAdapter(getContext(), dataList));

popupWindow = new PopupWindow(contentView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

popupWindow.setBackgroundDrawable(getResources().getDrawable(R.color.transparent));

popupWindow.setOutsideTouchable(true);

popupWindow.showAsDropDown(this);

* 关闭下拉列表弹窗

private void closePopWindow(){

popupWindow.dismiss();

popupWindow = null;

* 设置数据

* @param list

public void setItemsData(ArrayListString list){

dataList = list;

editText.setText(list.get(0).toString());

* 数据适配器

* @author caizhiming

class XCDropDownListAdapter extends BaseAdapter{

Context mContext;

ArrayListString mData;

LayoutInflater inflater;

public XCDropDownListAdapter(Context ctx,ArrayListString data){

mContext = ctx;

mData = data;

inflater = LayoutInflater.from(mContext);

@Override

public int getCount() {

// TODO Auto-generated method stub

return mData.size();

@Override

public Object getItem(int position) {

// TODO Auto-generated method stub

return null;

@Override

public long getItemId(int position) {

// TODO Auto-generated method stub

return position;

@Override

public View getView(int position, View convertView, ViewGroup parent) {

// TODO Auto-generated method stub

// 自定义视图

ListItemView listItemView = null;

if (convertView == null) {

// 获取list_item布局文件的视图

convertView = inflater.inflate(R.layout.dropdown_list_item, null);

listItemView = new ListItemView();

// 获取控件对象

listItemView.tv = (TextView) convertView

.findViewById(R.id.tv);

listItemView.layout = (LinearLayout) convertView.findViewById(R.id.layout_container);

// 设置控件集到convertView

convertView.setTag(listItemView);

} else {

listItemView = (ListItemView) convertView.getTag();

// 设置数据

listItemView.tv.setText(mData.get(position).toString());

final String text = mData.get(position).toString();

listItemView.layout.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

editText.setText(text);

closePopWindow();

return convertView;

private static class ListItemView{

TextView tv;

LinearLayout layout;

三、如何使用该自定义下拉列表框控件

使用该控件和使用普通的自带的控件一样,首先需要在布局文件中引用该控件:

RelativeLayout xmlns:android=""

xmlns:tools=""

android:id="@+id/container"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="com.czm.xcdropdownlistview.MainActivity"

tools:ignore="MergeRootFrame"

com.czm.xcdropdownlistview.XCDropDownListView

android:id="@+id/drop_down_list_view"

android:layout_marginTop="10dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true" /

/RelativeLayout

其次,就是在代码中使用该控件:

package com.czm.xcdropdownlistview;

import java.util.ArrayList;

import android.app.Activity;

import android.os.Bundle;

* 使用下拉列表框控件 示例

* @author caizhiming

public class MainActivity extends Activity {

XCDropDownListView dropDownListView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

dropDownListView = (XCDropDownListView)findViewById(R.id.drop_down_list_view);

ArrayListString list = new ArrayListString();

for(int i = 0;i 6;i++){

list.add("下拉列表项"+(i+1));

dropDownListView.setItemsData(list);

对了,这个控件中,我没有实现点击item项回调接口,这个可能对有些写惯了回调的可能觉得少了写什么的感觉,有兴趣的你可以自己添加相关回调操作哈,这个大家应该都会把。

android 系统源码修改 去除下拉菜单,底部菜单

android手机系统的顶部,点击菜单栏时,会有一个可以下拉的下拉菜单,,,其实就是他的下拉通知栏,,怎么实现这个下拉的功能

没分了 见谅

  安卓分类下拉菜单源码 


评论


最新评论