资讯

精准传达 • 有效沟通

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

android菜单,Android菜单组件

Android中怎么实现底部菜单栏

一共两种实现方式:

创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于成都网站设计、成都做网站、外贸网站建设、右玉网络推广、成都小程序开发、右玉网络营销、右玉企业策划、右玉品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联公司为所有大学生创业者提供右玉建站搭建服务,24小时服务热线:18982081108,官方网址:www.cdcxhl.com

方式一:java代码实现

@Override

public boolean onCreateOptionsMenu(Menu menu) {

/**

* 参数1:组的id

* 参数2:菜单的id

* 参数3:排列的顺序

* 参数4:显示菜单的文本

*/

menu.add(0, 0, 0, "你好");

menu.add(1, 1, 1, "不好");

return super.onCreateOptionsMenu(menu);

}

/**菜单的点击事件*/

@Override

public boolean onOptionsItemSelected(MenuItem item) {

switch(item.getItemId()){

case 0:

Toast.makeText(this, "你选择你好", Toast.LENGTH_LONG).show();

break;

case 1:

Toast.makeText(this, "你选择不好", Toast.LENGTH_LONG).show();

break;

}

}

方式二:xml形式的菜单

步骤一:在res/menu目录下新建xml文件

menu xmlns:android=""

xmlns:tools=""

tools:context="com.lifei.menu.MainActivity"

item

android:id="@+id/delete_menu_item"

android:orderInCategory="20"

android:title="@string/deletemunu"/

item

android:id="@+id/add_menu_item"

android:orderInCategory="19"

android:title="@string/addmunu"

menu

item

android:id="@+id/add_user_item"

android:orderInCategory="30"

android:title="@string/adduser"/

item

android:id="@+id/add_phone_item"

android:orderInCategory="31"

android:showAsAction="never"

android:title="@string/addhpone"/

/menu

/item

/menu

步骤二:在activity中

@Override

public boolean onCreateOptionsMenu(Menu menu) {

/**xml形式的menu菜单*/

getMenuInflater().inflate(R.menu.main, menu);

return super.onCreateOptionsMenu(menu);

}

点击事件与方式一是一样的

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中怎么让menu菜单显示在屏幕左上角

android 中让菜单menu显示在左上角,可以使用popupwindow技术,也就是悬浮菜单,设置默认的位置为左上角,如下代码:

package com.example.menutype;

import android.app.ActionBar.LayoutParams;

import android.app.Activity;

import android.content.Context;

import android.os.Bundle;

import android.view.Gravity;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.PopupWindow;

import android.widget.PopupWindow.OnDismissListener;

import android.widget.TextView;

import android.widget.Toast;

/**

* @author tr

* @time 2014-3-10

* @description 自定义菜单,下拉菜单样式,添加动画效果,重写onMenuOpened()方法,自定义"menu"按键弹出菜单

*/

public class MainActivity extends Activity implements OnClickListener{

private static Toast mToast;

private static Context mContext;

private PopupWindow popupWindow ;

private Button btn_popupwindow;

private View mPopupWindowView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mContext = this;

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

btn_popupwindow.setOnClickListener(this);

initPopupWindow();

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

/**actionBar上更多按钮*/

getMenuInflater().inflate(R.menu.main, menu);

/**点击menu,弹出菜单*/

/*

* add()方法的四个参数,依次是:

* 1、组别,如果不分组的话就写Menu.NONE,

* 2、Id,这个很重要,Android根据这个Id来确定不同的菜单

* 3、顺序,那个菜单现在在前面由这个参数的大小决定

* 4、文本,菜单的显示文本

*/

menu.add(Menu.NONE, Menu.FIRST + 1, 1, getResource(R.string.edit_text)).setIcon(

R.drawable.ic_edit);

// setIcon()方法为菜单设置图标,这里使用的是系统自带的图标,同学们留意一下,以

// android.R开头的资源是系统提供的,我们自己提供的资源是以R开头的

menu.add(Menu.NONE, Menu.FIRST + 2, 2, getResource(R.string.file)).setIcon(

R.drawable.ic_menu_file);

menu.add(Menu.NONE, Menu.FIRST + 3, 3, getResource(R.string.about)).setIcon(

R.drawable.ic_menu_about);

return true;

}

/**菜单打开时调用*/

@Override

public boolean onMenuOpened(int featureId, Menu menu) {

// TODO Auto-generated method stub

showToast("menu菜单打开:"+featureId);

//点击"menu"按钮打开

if(featureId == 0){

showPopupWindow();

}

return super.onMenuOpened(featureId, menu);// 返回为true 则显示系统menu

// return false;

}

/**menu菜单关闭时调用*/

@Override

public void onOptionsMenuClosed(Menu menu) {

// TODO Auto-generated method stub

super.onOptionsMenuClosed(menu);

showToast("menu菜单关闭");

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

// TODO Auto-generated method stub

switch(item.getItemId()){

case Menu.FIRST + 1:

case R.id.action_edit:

showToast(getResource(R.string.edit_text));

break;

case Menu.FIRST + 2:

case R.id.action_file:

showToast(getResource(R.string.file));

break;

case R.id.action_favorite:

showToast(getResource(R.string.favorite));

break;

case R.id.action_share:

showToast(getResource(R.string.share));

break;

case Menu.FIRST + 3:

case R.id.action_about:

showToast(getResource(R.string.about));

break;

}

return super.onOptionsItemSelected(item);

}

@Override

public void onClick(View v) {

switch(v.getId()){

case R.id.btn_popupwindow:

showPopupWindow();

break;

case R.id.textview_about:

showToast(getResource(R.string.about));

popupWindow.dismiss();

break;

case R.id.textview_edit:

showToast(getResource(R.string.edit_text));

popupWindow.dismiss();

break;

case R.id.textview_file:

showToast(getResource(R.string.file));

popupWindow.dismiss();

break;

}

}

/**显示popupwindow*/

private void showPopupWindow(){

if(!popupWindow.isShowing()){

popupWindow.showAsDropDown(btn_popupwindow, btn_popupwindow.getLayoutParams().width/2, 0);

}else{

popupWindow.dismiss();

}

}

/**

* 初始化popupwindow

*/

private void initPopupWindow(){

initPopupWindowView();

//初始化popupwindow,绑定显示view,设置该view的宽度/高度

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

popupWindow.setFocusable(true);

popupWindow.setOutsideTouchable(true);

// 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景;使用该方法点击窗体之外,才可关闭窗体

popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.bitmap_book_read_chapterlist_repeat));

//Background不能设置为null,dismiss会失效

// popupWindow.setBackgroundDrawable(null);

//设置渐入、渐出动画效果

// popupWindow.setAnimationStyle(R.style.popupwindow);

popupWindow.update();

//popupWindow调用dismiss时触发,设置了setOutsideTouchable(true),点击view之外/按键back的地方也会触发

popupWindow.setOnDismissListener(new OnDismissListener() {

@Override

public void onDismiss() {

// TODO Auto-generated method stub

// showToast("关闭popupwindow");

}

});

}


分享题目:android菜单,Android菜单组件
当前网址:http://cdkjz.cn/article/dsgdsij.html
多年建站经验

多一份参考,总有益处

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

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

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220