popupwindow是安卓中的一个弹出窗口类,可以弹出指定的一个布局文件,还是很方便的,下面演示如何制作一个微信的右上角点击弹出菜单。
首先定义首页布局,默认取消标题栏
找到styles.xml文件,将parent属性设置为Theme.AppCompat.Light.NoActionBar,这样就取消了原本的标题栏了。
如图:
选择布局文件夹,创建一个新的布局文件,我起名叫做menu,名字随你喜欢
下面贴出布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
>
<!--右侧位置-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="#333333"
android:drawableLeft="@drawable/icon_group"
android:text="群组"
android:textColor="#ffffff"
android:paddingRight="40dp"
android:paddingLeft="10dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="#333333"
android:drawableLeft="@drawable/icon_add"
android:text="群组"
android:textColor="#ffffff"
android:paddingRight="40dp"
android:paddingLeft="10dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="#333333"
android:drawableLeft="@drawable/icon_card"
android:text="我的卡包"
android:textColor="#ffffff"
android:paddingRight="12dp"
android:paddingLeft="10dp"
/>
</LinearLayout>
我是使用的是按钮方案作为菜单的,不是列表,这样做的好处是方便添加按钮,并且我们弹出的菜单不多,就不需要使用到列表了。其中值得注意的是,文字设置如果字数不一样,需要使用paddingRight进行微调,边距代码无需过多说明,按钮的背景图片就是图标,后面会打包给大家。
布局效果图片:
制作好了菜单布局以后,接着制作首页界面,可以看到需要一个标题,然后一个图标按钮。
创建一个TextView,宽度设置为和窗口一样宽,高度设置为40dp,内边距根据需要设置即可。再添加一个按钮,设置layout_alignParentRight属性为true,按钮就会浮动到最右边了
下面贴出主布局的代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="cn.mycsdn.weimenu.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="@string/testtitle"
android:paddingTop="5dp"
android:background="#1a1c1d"
android:textColor="#ffffff"
android:paddingLeft="20dp"
android:textSize="25sp"
/>
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/btn_menu"
android:layout_alignParentRight="true"
android:onClick="Onmenu"
android:background="@drawable/btn_add"
/>
<!--下面是一个提示,要不要无所谓-->
<TextView
android:text="微信弹出菜单模仿"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/textView"
android:textColor="@android:color/holo_blue_dark"
android:textSize="30sp" />
</RelativeLayout>
这儿为点击的按钮设置了点击事件,就不需要再去Java代码绑定了,免得麻烦。事件名称为:Onmenu。
在MainActivity.java中添加Onmenu方法。
public void Onmenu(View v){
View popupwindow_menu=getLayoutInflater().inflate(R.layout.menu,null,false);
pop=new PopupWindow(popupwindow_menu, ActionBar.LayoutParams.WRAP_CONTENT,ActionBar.LayoutParams.WRAP_CONTENT,true);
pop.showAsDropDown(findViewById(R.id.btn_menu),0,0);
}
创建一个布局文件,使用getLayoutInflater().inflate来载入我们创建的一个布局文件,其中pop为在加载主方法的时候定义了一个PopupWindow类,要先定义好,以免点击事件发生的时候多次创建。
PopupWindow构造函数可以传入初始化参数,PopupWindow(View contentView, int width, int height) ,参数一是view,参数二是宽度,参数三是高度
关于更多的知识可以看看这一文章:http://blog.csdn.net/harvic880925/article/details/49272285
最终的弹出方法是pop.showAsDropDown。
值得注意的
安卓5.0,是不会点击其他地方这个菜单消失的,所以,我们需要兼容5.0版本的话,要给代码加上点击其他地方消失,设置setOnTouchListener方法,重写onTouch方法。判断pop不为空,并且pop正显示。判断有的话,先隐藏pop,并且销毁他。
popupwindow_menu.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if (pop!=null && pop.isShowing()){
pop.dismiss();
pop=null;
}
return false;
}
});
最后需要给菜单绑定上事件,我这儿偷个懒就直接给onclick加上了,你们可以自己去重写onclick类
public void qunzu(View v){
Toast.makeText(this,"我是群组",Toast.LENGTH_SHORT).show();
pop.dismiss();
}
点击第一个菜单后,就会弹出一个提示了
本文介绍了如何使用Android的PopupWindow类创建一个模仿微信右上角菜单的效果。通过设置布局文件,取消标题栏,定义菜单布局和主界面布局,实现点击按钮弹出菜单。在MainActivity.java中处理点击事件,并确保在Android 5.0及以上版本中,点击屏幕其他区域能关闭菜单。提供了完整的代码下载链接。

被折叠的 条评论
为什么被折叠?



