PopuMenu 顾名思义就是弹出菜单 当用户触碰了屏幕上的一个控件(TextView button 等)时弹出一个菜单了供选择。
案例是一个实现天气选择的功能
直接上代码:
布局文件很简单,就是一个图片视图和一个TextView 当用户触碰这个TextView 时弹出菜单让用户选择天气,然后根据用户的选择变更图片显示。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Switch android:id="@+id/switch_button" android:layout_width="120dip" android:layout_height="50dip" android:typeface="monospace" android:layout_gravity="center_horizontal" /> <ImageView android:id="@+id/bulb_imageview" android:layout_width="189dip" android:layout_height="275dip" android:layout_gravity="center_horizontal" android:src="@drawable/p1" /> </LinearLayout>
package com.sane.popumenudemo; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ImageView; import android.widget.PopupMenu; import android.widget.TextView; public class MainActivity extends Activity { private TextView mWeatherSelect; private ImageView mWeatherImageView; private static String CURRENT_WEATHER_STATE="currentWeather"; private int currentWeather; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState!=null&&savedInstanceState.containsKey(CURRENT_WEATHER_STATE)) { currentWeather=savedInstanceState.getInt(CURRENT_WEATHER_STATE); } mWeatherImageView=(ImageView) findViewById(R.id.weathear_imageview); mWeatherSelect=(TextView) findViewById(R.id.sweather_textview); if(currentWeather!=0){ mWeatherImageView.setImageResource(currentWeather); } mWeatherSelect.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PopupMenu popupMenu=new PopupMenu(MainActivity.this, mWeatherSelect); popupMenu.getMenuInflater().inflate(R.menu.main, popupMenu.getMenu()); popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.qing: mWeatherImageView.setImageResource(R.drawable.qing); currentWeather=R.drawable.qing; break; case R.id.duoyun: mWeatherImageView.setImageResource(R.drawable.duoyun); currentWeather=R.drawable.duoyun; break; case R.id.yin: mWeatherImageView.setImageResource(R.drawable.yin); currentWeather=R.drawable.yin; break; } return true; } }); popupMenu.show(); } }); } @Override protected void onSaveInstanceState(Bundle outState) { // TODO Auto-generated method stub outState.putInt(CURRENT_WEATHER_STATE, currentWeather); super.onSaveInstanceState(outState); } }
在这段代码中为了防止用户屏幕旋转导致的图片显示总是被刷新的问题,重写了Activity的onSaveInstanceState 方法用来保存用户点选的天气状态的记录。在OnCreate的时候就从Bundle savedInstanceState 中获取出来填充上去。从而解决了用户旋转手机而导致的数据不能被保存的问题。
![](https://i-blog.csdnimg.cn/blog_migrate/a35c8026d04ba68397e4f85a4ee3c37f.png)
![](https://i-blog.csdnimg.cn/blog_migrate/93af231e9cd80d2499f1296aebc720ea.png)
![](https://i-blog.csdnimg.cn/blog_migrate/cf97c79ffa88566eabbff529786b2f83.png)