Android popu菜单
来源:互联网 发布:软件测试有哪些工具 编辑:程序博客网 时间:2024/05/06 12:45
popu 菜单构建与contextMenu 菜单以及 optionsMenu 菜单略微不同
使用步骤为
① 创建popu 对象
② 设置监听
③ 调用popu的show() 方法
package com.example.gp11_day12_popupmenu;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView;private float fontSize = 20f;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.textView); textView.setTextSize(fontSize);}//当点击按钮时显示弹出菜单@SuppressLint("NewApi")public void showMenu(View v){ //创建弹出菜单 //第二个参数接收的是一个视图对象,作用是指定弹出菜单显示的参考位置 PopupMenu pop = new PopupMenu(this, v); //生成菜单项对象并加入到弹出菜单中 getMenuInflater().inflate(R.menu.main, pop.getMenu()); //给菜单添加事件监听 pop.setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { int id = item.getItemId(); switch(id) { case R.id.action_bigFont: fontSize +=5; textView.setTextSize(fontSize); break; case R.id.action_smallFont: fontSize -=5; textView.setTextSize(fontSize); break; case R.id.action_bgColor: int red = (int)(Math.random()*256); int green = (int)(Math.random()*256); int blue = (int)(Math.random()*256); textView.setBackgroundColor(Color.rgb(red, green, blue)); break; } return false; } }); //显示弹出菜单 pop.show();}
}
0 0
- Android popu菜单
- Android Menu,OptionMenu选项菜单,ContextMenu上下文菜单,Contextual ActionMode,PopUp Menu,PopU pWi ndow
- android菜单子菜单
- Android菜单
- android 菜单
- Android菜单
- android 菜单
- Android 菜单
- Android菜单
- Android菜单
- android--菜单
- Android菜单
- Android菜单
- Android菜单
- android 菜单
- android菜单
- android菜单
- android菜单
- WinXP下安装Linux Mint 17.2
- 2016.3.5 第一次Java实验
- LeetCode : Integer to Roman [java]
- C++未经初始化
- 【C#学习】从泛型集合到强制转换,start brain storm~
- Android popu菜单
- linux shell脚本 -- 定时任务--备份日志
- WKWebView与JS的交互
- android的context使用,工具类context
- MessageBox语法介绍 【精华】
- 不知为何感觉百度脑图并没有想象中那种功能的说。。。
- BZOJ_P3680 吊打XXX(模拟退火)
- 经典排序算法——快速排序
- Android开源框架ViewPageIndicator和Viewpage