iconContextMenu-古蔺大橙子建站
RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
iconContextMenu

点击一个控件后,在屏幕中央

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:国际域名空间、网页空间、营销软件、网站建设、龙岗网站维护、网站推广。

弹出一个类似menu菜单栏

package com.example.contextmenu;

import android.app.Activity;

import android.content.Context;

import android.content.res.Resources;

import android.os.Bundle;

import android.util.Log;

import android.view.Menu;

import android.view.SubMenu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;

public class ContextActivity extends Activity implements OnClickListener{

private final int CONTEXT_MENU_ID = 1;

private static final int CTX_MENU_ITEM_ADD = Menu.FIRST;// 

private static final int CTX_MENU_ITEM_DELETE = Menu.FIRST + 1;

private static final int CTX_MENU_ITEM_FIND = Menu.FIRST + 2;

private IconContextMenu iconContextMenu;

private Button btn;

private Context mContext;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_context);

mContext = this;

btn = (Button) findViewById(R.id.button1);

btn.setOnClickListener(this);

}

private void showContext(){

Resources res = getResources();

// init the menu

if (iconContextMenu == null)

iconContextMenu = new IconContextMenu(this, CONTEXT_MENU_ID);

iconContextMenu.clearItems();

R.drawable.revert, CTX_MENU_ITEM_ADD);

iconContextMenu.addItem(res, getText(R.string.delete),//

R.drawable.play , CTX_MENU_ITEM_DELETE);

iconContextMenu.addItem(res, getText(R.string.find),//

R.drawable.question, CTX_MENU_ITEM_FIND);

iconContextMenu.setOnClickListener(new IconContextMenu.IconContextMenuOnClickListener() {

@Override

public void onClick(int menuId) {

// TODO Auto-generated method stub

String str = "null";

switch(menuId){

case CTX_MENU_ITEM_ADD:

str = "增加";

break;

case CTX_MENU_ITEM_DELETE:

str = "删除";

break;

case CTX_MENU_ITEM_FIND:

str = "查找";

break;

}

Log.v("iconcontext", str);

Toast.makeText(mContext, str, Toast.LENGTH_SHORT).show();

}

});

}

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

if(arg0.equals(btn)){

showContext();

iconContextMenu.createMenu("IconContextMenu").show();

}

}

}

//封装的类IconContextMenu 

package com.example.contextmenu;

import java.util.ArrayList;

import android.app.Activity;

import android.app.AlertDialog;

import android.app.Dialog;

import android.content.Context;

import android.content.DialogInterface;

import android.content.res.Resources;

import android.content.res.Resources.Theme;

import android.graphics.Color;

import android.graphics.drawable.Drawable;

import android.util.TypedValue;

import android.view.View;

import android.view.ViewGroup;

import android.widget.AbsListView;

import android.widget.BaseAdapter;

import android.widget.TextView;

public class IconContextMenu implements DialogInterface.OnCancelListener, DialogInterface.OnDismissListener {

//private static final int Build_VERSION_CODES_ICE_CREAM_SANDWICH = 14;

private static final int LIST_PREFERED_HEIGHT = 85;

private IconMenuAdapter menuAdapter = null;

private Activity parentActivity = null;

private IconContextMenuOnClickListener clickHandler = null;

/**

* constructor

* @param parent

* @param id

*/

public IconContextMenu(Activity parent, int id) {

this.parentActivity = parent;

menuAdapter = new IconMenuAdapter(parentActivity);

}

/**

* Add menu item

* @param menuItem

*/

public void addItem(Resources res, CharSequence title, int p_w_picpathResourceId, int actionTag) {

menuAdapter.addItem(new IconContextMenuItem(res, title, p_w_picpathResourceId, actionTag));

}

public void addItem(Resources res, int textResourceId, int p_w_picpathResourceId, int actionTag) {

menuAdapter.addItem(new IconContextMenuItem(res, textResourceId, p_w_picpathResourceId, actionTag));

}

public void clearItems() {

menuAdapter.clearItems();

}

/**

* Set menu onclick listener

* @param listener

*/

public void setOnClickListener(IconContextMenuOnClickListener listener) {

clickHandler = listener;

}

/**

* Create menu

* @return

*/

public Dialog createMenu(String menuItitle) {

final AlertDialog.Builder builder = new AlertDialog.Builder(parentActivity);

builder.setTitle(menuItitle);

builder.setAdapter(menuAdapter, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialoginterface, int i) {

IconContextMenuItem item = (IconContextMenuItem) menuAdapter.getItem(i);

if (clickHandler != null) {

clickHandler.onClick(item.actionTag);

}

}

});

builder.setInverseBackgroundForced(true);

AlertDialog dialog = builder.create();

dialog.setOnCancelListener(this);

dialog.setOnDismissListener(this);

return dialog;

}

public void onCancel(DialogInterface dialog) {

cleanup();

}

public void onDismiss(DialogInterface dialog) {

}

private void cleanup() {

// parentActivity.dismissDialog(dialogId);

}

/**

* IconContextMenu On Click Listener interface

*/

public interface IconContextMenuOnClickListener {

public abstract void onClick(int menuId);

}

/**

* Menu-like list adapter with icon

*/

protected class IconMenuAdapter extends BaseAdapter {

private Context context = null;

private ArrayList mItems = new ArrayList();

public IconMenuAdapter(Context context) {

this.context = context;

}

/**

* add item to adapter

* @param menuItem

*/

public void addItem(IconContextMenuItem menuItem) {

mItems.add(menuItem);

}

public void clearItems() {

mItems.clear();

}

@Override

public int getCount() {

return mItems.size();

}

@Override

public Object getItem(int position) {

return mItems.get(position);

}

@Override

public long getItemId(int position) {

IconContextMenuItem item = (IconContextMenuItem) getItem(position);

return item.actionTag;

}

@SuppressWarnings("deprecation")

@Override

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

IconContextMenuItem item = (IconContextMenuItem) getItem(position);

Resources res = parentActivity.getResources();

if (convertView == null) {

TextView temp = new TextView(context);

AbsListView.LayoutParams param = new AbsListView.LayoutParams(AbsListView.LayoutParams.FILL_PARENT, 

AbsListView.LayoutParams.WRAP_CONTENT);

temp.setLayoutParams(param);

temp.setPadding((int) toPixel(res, 15), 0, (int) toPixel(res, 15), 0);

temp.setGravity(android.view.Gravity.CENTER_VERTICAL);

Theme th = context.getTheme();

TypedValue tv = new TypedValue();

if (th.resolveAttribute(android.R.attr.textAppearanceLargeInverse, tv, true)) {

temp.setTextAppearance(context, tv.resourceId);

}

temp.setMinHeight(LIST_PREFERED_HEIGHT);

temp.setCompoundDrawablePadding((int) toPixel(res, 20));

convertView = temp;

}

TextView textView = (TextView) convertView;

textView.setTag(item);

textView.setText(item.text);

textView.setTextColor(Color.BLACK);//刚开始,这里没有为自体设置颜色,导致文字看不出来

textView.setCompoundDrawablesWithIntrinsicBounds(item.p_w_picpath, null, null, null);

return textView;

}

private float toPixel(Resources res, int dip) {

float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, res.getDisplayMetrics());

return px;

}

}

/**

* menu-like list item with icon

*/

protected class IconContextMenuItem {

public final CharSequence text;

public final Drawable p_w_picpath;

public final int actionTag;

/**

* public constructor

* @param res

*            resource handler

* @param textResourceId

*            id of title in resource

* @param p_w_picpathResourceId

*            id of icon in resource

* @param actionTag

*            indicate action of menu item

*/

public IconContextMenuItem(Resources res, int textResourceId, int p_w_picpathResourceId, int actionTag) {

text = res.getString(textResourceId);

if (p_w_picpathResourceId != -1) {

p_w_picpath = res.getDrawable(p_w_picpathResourceId);

} else {

p_w_picpath = null;

}

this.actionTag = actionTag;

}

/**

* public constructor

* @param res

*            resource handler

* @param title

*            menu item title

* @param p_w_picpathResourceId

*            id of icon in resource

* @param actionTag

*            indicate action of menu item

*/

public IconContextMenuItem(Resources res, CharSequence title, int p_w_picpathResourceId, int actionTag) {

text = title;

if (p_w_picpathResourceId != -1) {

p_w_picpath = res.getDrawable(p_w_picpathResourceId);

} else {

p_w_picpath = null;

}

this.actionTag = actionTag;

}

}

}

//activity_context.xml 里面只有一个Button按钮

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".ContextActivity" >

   

        android:id="@+id/button1"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="Button" />


本文名称:iconContextMenu
当前URL:http://scgulin.cn/article/iissop.html