资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

android弹出菜单,android菜单栏

android如何弹出一个占屏幕一半的菜单

android弹出一个占屏幕一半的菜单,可以使用popupwindow,设置弹出的xy轴的距离占据屏幕一半即可,如下代码:

公司主营业务:成都网站设计、网站建设、外贸网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联建站是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联建站推出望江免费做网站回馈大家。

package com.example.hellopopupwindow;

import android.os.Bundle;

import android.app.Activity;

import android.content.Context;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.MotionEvent;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.View.OnTouchListener;

import android.view.ViewGroup.LayoutParams;

import android.widget.Button;

import android.widget.PopupWindow;

import android.widget.Toast;

public class MainActivity extends Activity {

private Context mContext = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mContext = this;

Button button = (Button) findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

showPopupWindow(view);

}

});

}

private void showPopupWindow(View view) {

// 一个自定义的布局,作为显示的内容

View contentView = LayoutInflater.from(mContext).inflate(

R.layout.pop_window, null);

// 设置按钮的点击事件

Button button = (Button) contentView.findViewById(R.id.button1);

button.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Toast.makeText(mContext, "button is pressed",

Toast.LENGTH_SHORT).show();

}

});

final PopupWindow popupWindow = new PopupWindow(contentView,

LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);

popupWindow.setTouchable(true);

popupWindow.setTouchInterceptor(new OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

Log.i("mengdd", "onTouch : ");

return false;

// 这里如果返回true的话,touch事件将被拦截

// 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss

}

});

// 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框

// 我觉得这里是API的一个bug

popupWindow.setBackgroundDrawable(getResources().getDrawable(

R.drawable.selectmenu_bg_downward));

// 设置好参数之后再show

popupWindow.showAsDropDown(view);

}

}

Android 仿微信长按列表弹出PopupMenu菜单栏

弹出效果如下图所示

可以使用setGravity()方法来指定弹出窗口与anchor视图的对齐方式,例如修改对齐方式为Gravity.END

使用起来还是比较简单的,但是好像大部分项目的需求是PopupMenu在用户点击的位置弹出,然而PopupMenu并没有提供在指定坐标弹出的方法,所以只能咱们自己来实现咯!

想让PopupMenu在指定弹出位置,首先咱们得先了解show()方法是如何让PopupMenu弹出来的,所以只能去阅读源码了(Read The Fucking Source Code~)。

PopupMenu的show()方法很简单,直接把任务转给MenuPopupHelper来处理,处理流程:show() - tryShow() - showPopup(0, 0, false, false);

我们可以看到showPopup方法内有两个参数int xOffset、int yOffset,根据注释可以知道这就是相对于anchor视图的坐标值。所以如果要指定PopupMenu的弹出位置,MenuPopupHelper应该这样处理弹出逻辑:show(int x, int y) - tryShow(int x, int y) - showPopup(x, y, true, true)。

但是由于PopupMenu无法调用到MenuPopupHelper的show(int x, int y) 方法,因此我们只能使用反射机制绕过PopupMenu,直接调用MenuPopupHelper的show(int x, int y)方法。

到此为止,已经有了大致的解决思路,接下来看看具体实现。

最终弹出效果如下图所示

android 点击按钮时显示菜单应怎样实现?

点击button弹出对话框菜单

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

publicclass choice extends Activity {

private Button button;

/** Called when the activity is first created. */

@Override

publicvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

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

button.setOnClickListener(new OnClickListener(){

@Override

publicvoid onClick(View arg0) {

new AlertDialog.Builder(choice.this)

.setTitle("choice")

.setItems(R.array.str_body, new DialogInterface.OnClickListener() {

@Override

publicvoid onClick(DialogInterface arg0, int arg1) {

// TODO Auto-generated method stub

String[] aryshop=getResources().getStringArray(R.array.str_body);

new AlertDialog.Builder(choice.this)

.setMessage(aryshop[arg1])

.setNegativeButton("ok", new DialogInterface.OnClickListener() {

@Override

publicvoid onClick(DialogInterface arg0, int arg1) {

// TODO Auto-generated method stub

}

}).show();

}

}).show();

// TODO Auto-generated method stub

}});

}

}

菜单项

?xmlversion="1.0"encoding="utf-8"?

resources

stringname="hello"Hello World, choice!/string

stringname="app_name"ChoiceMenu/string

stringname="strtitle"按我选择:/string

stringname="str"你选择的是:/string

arrayname="str_body"

item选项1/item

item选项2/item

item选项3/item

item选项4/item

item选项5/item

item选项6/item

/array

/resources


新闻标题:android弹出菜单,android菜单栏
本文来源:http://cdkjz.cn/article/dsicjsp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220