android教程之使用popupwindow创建菜单示例
时间:2023-01-24 22:08:10
PopupWindow是一个弹出式窗口,它可以展示任意View。他会浮在当前窗口的上方展示。
下面看代码:
public class MyActivity extends Activity{
private PopupWindow menu;
private LayoutInflater inflater;
private View layout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//实例化PopupWindow创建菜单
initMenu();
}
//判断按键 菜单的显示与隐藏
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(!menu.isShowing()&&keyCode == KeyEvent.KEYCODE_MENU){
show();
}else{
menu.dismiss();
}
if(keyCode == KeyEvent.KEYCODE_BACK&&menu.isShowing()){
menu.dismiss();
}
return true;
}
//实例化PopupWindow创建菜单
private void initMenu(){
//获取LayoutInflater实例
inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
//获取弹出菜单的布局
layout = inflater.inflate(R.layout.menu,null);
//设置popupWindow的布局
menu = new PopupWindow(layout, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT);
}
//显示菜单
private void show(){
//设置位置
menu.showAtLocation(this.findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL,0,0); //设置在屏幕中的显示位置
}
}
菜单的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/icon_1"
android:background="@drawable/icon_1"
android:layout_width="40dp"
android:layout_height="40dp"/>
<ImageView
android:id="@+id/icon_2"
android:background="@drawable/icon_2"
android:layout_width="40dp"
android:layout_height="40dp"/>
<ImageView
android:id="@+id/icon_3"
android:background="@drawable/icon3"
android:layout_width="40dp"
android:layout_height="40dp"/>
<ImageView
android:id="@+id/icon_4"
android:background="@drawable/icon4"
android:layout_width="40dp"
android:layout_height="40dp"/>
</LinearLayout>
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java持久化框架Hibernate与Mybatis优劣及选择详解
Android实现通用筛选栏
![](https://img.aspxhome.com/file/2023/3/117073_0s.gif)
Android自定义View实现带数字的进度条实例代码
![](https://img.aspxhome.com/file/2023/4/91704_0s.gif)
SpringBoot项目中的多数据源支持的方法
理解C#中的Lambda表达式
SpringBoot集成JmsTemplate(队列模式和主题模式)及xml和JavaConfig配置详解
![](https://img.aspxhome.com/file/2023/5/64025_0s.png)
Android中SparseArray性能优化的使用方法
![](https://img.aspxhome.com/file/2023/8/139318_0s.jpg)
Android引用开源框架通过AsyncHttpClient实现文件上传
![](https://img.aspxhome.com/file/2023/9/130429_0s.jpg)
解决微服务中关于用户token处理到的坑
详解Java实现数据结构之并查集
![](https://img.aspxhome.com/file/2023/6/67976_0s.jpg)
Java利用三目运算符比较三个数字的大小
java贪吃蛇游戏编写代码
![](https://img.aspxhome.com/file/2023/3/74753_0s.jpg)
一文带你快速了解java中的static关键词
Javassist如何操作Java 字节码
![](https://img.aspxhome.com/file/2023/2/76582_0s.jpg)
导入项目出现Java多个工程相互引用异常A cycle was detected in the build path of project的解决办法
![](https://img.aspxhome.com/file/2023/5/101935_0s.png)
Android Handler移除Message详解及实例代码
Android 7.0行为变更 FileUriExposedException解决方法
![](https://img.aspxhome.com/file/2023/7/110697_0s.png)
基于java math API 的详细解释说明
Android Studio和阿里云数据库实现一个远程聊天程序
![](https://img.aspxhome.com/file/2023/4/92614_0s.jpg)
浅谈Android的Lifecycle源码分析
![](https://img.aspxhome.com/file/2023/2/137542_0s.jpg)