码迷,mamicode.com
首页 > 其他好文 > 详细

popupMenu-----弹出菜单

时间:2015-09-19 22:35:50      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
 1 import android.os.Bundle;
 2 import android.app.Activity;
 3 import android.graphics.Color;
 4 import android.view.Menu;
 5 import android.view.MenuItem;
 6 import android.view.View;
 7 import android.widget.PopupMenu;
 8 import android.widget.PopupMenu.OnMenuItemClickListener;
 9 import android.widget.TextView;
10 
11 public class MainActivity extends Activity {
12 
13     private TextView textView;
14     private float fontSize =15;
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_main);
19         
20         textView = (TextView) findViewById(R.id.textView);
21     }
22     //弹出菜单是3.0开始出现的,所以api最低为11
23     public void btnClick(View v)
24     {
25         //第二个参数是弹出菜单显示的参考位置,通常在参考视图的下边显示
26         //创建一个弹出菜单对象
27         PopupMenu  pop = new PopupMenu(this, v);
28         
29         //给菜单添加菜单项
30         getMenuInflater().inflate(R.menu.main, pop.getMenu());
31         
32         //给菜单项添加事件监听
33         pop.setOnMenuItemClickListener(new OnMenuItemClickListener() {
34             
35             @Override
36             public boolean onMenuItemClick(MenuItem item) {
37                 switch(item.getItemId())
38                 {
39                     case R.id.action_add:
40                         fontSize+=5;
41                         textView.setTextSize(fontSize);
42                         break;
43                     case R.id.action_sub:
44                         fontSize-=5;
45                         textView.setTextSize(fontSize);
46                         break;
47                     case R.id.action_background:
48                         int red = (int)(Math.random()*256);
49                         int green = (int)(Math.random()*256);
50                         int blue = (int)(Math.random()*256);
51                         textView.setBackgroundColor(Color.rgb(red, green, blue));
52                         break;
53                 }
54                 return false;
55             }
56         });
57         
58         //显示菜单
59         pop.show();
60     }
61 
62 }
MainActivity
技术分享
 1 <menu xmlns:android="http://schemas.android.com/apk/res/android" >
 2 
 3      <item
 4         android:id="@+id/action_add"
 5         android:orderInCategory="100"
 6         android:showAsAction="never"
 7         android:title="放大字体"/>
 8     <item
 9         android:id="@+id/action_sub"
10         android:orderInCategory="100"
11         android:showAsAction="never"
12         android:title="缩小字体"/>
13     <item
14         android:id="@+id/action_background"
15         android:orderInCategory="100"
16         android:showAsAction="never"
17         android:title="改变背景颜色"/>
18 
19 </menu>
menu_main

 

popupMenu-----弹出菜单

标签:

原文地址:http://www.cnblogs.com/bimingcong/p/4822396.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!