安卓仿微信菜单栏PopWindow
来源:互联网 发布:yu家it服务和数据分析 编辑:程序博客网 时间:2024/06/01 15:14
先构建好xml布局文件 用于弹窗的布局文件
<?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:layout_marginRight="5dp" android:layout_marginTop="20dp" android:background="@drawable/popmenu" android:orientation="vertical"> <LinearLayout android:id="@+id/ll_popmenu_record" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:gravity="center_vertical" android:orientation="horizontal" android:paddingLeft="10dp"> <ImageView android:layout_width="25dp" android:layout_height="25dp" android:src="@mipmap/add" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:gravity="center_vertical" android:text="记一笔" android:textColor="@color/fffwhite" android:textSize="20sp" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="0.7dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="@color/fffwhite" /> <LinearLayout android:id="@+id/ll_popmenu_book" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginLeft="10dp" android:layout_marginTop="8dp" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:layout_width="25dp" android:layout_height="25dp" android:src="@mipmap/book" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:gravity="center_vertical" android:text="账本切换" android:textColor="@color/fffwhite" android:textSize="20sp" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="0.7dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="@color/fffwhite" /> <LinearLayout android:id="@+id/ll_popmenu_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="8dp" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:layout_width="25dp" android:layout_height="25dp" android:src="@mipmap/search" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:gravity="center_vertical" android:text="搜索账本" android:textColor="@color/fffwhite" android:textSize="20sp" /> </LinearLayout></LinearLayout>
新建TopPopWindow extends PopupWindow。
package views;import android.app.Activity;import android.graphics.drawable.ColorDrawable;import android.view.LayoutInflater;import android.view.View;import android.widget.LinearLayout;import android.widget.PopupWindow;import com.chase.cn.money_of_my.R;/** * Created by Chase on 2017/2/23. */public class TopPopWindow extends PopupWindow { private View mView; private LinearLayout ll_popmenu_record,ll_popmenu_book,ll_popmenu_search; public TopPopWindow(Activity paramActivity, View.OnClickListener paramOnClickListener, int paramInt1, int paramInt2){ mView = LayoutInflater.from(paramActivity).inflate(R.layout.popwindow_topright, null); ll_popmenu_record = (LinearLayout) mView.findViewById(R.id.ll_popmenu_record); ll_popmenu_book = (LinearLayout) mView.findViewById(R.id.ll_popmenu_book); ll_popmenu_search = (LinearLayout) mView.findViewById(R.id.ll_popmenu_search); if (paramOnClickListener != null){ //设置点击监听 ll_popmenu_record.setOnClickListener(paramOnClickListener); ll_popmenu_book.setOnClickListener(paramOnClickListener); ll_popmenu_search.setOnClickListener(paramOnClickListener); setContentView(mView); //设置宽度 setWidth(paramInt1); //设置高度 setHeight(paramInt2); //设置显示隐藏动画 setAnimationStyle(R.style.AnimTools); //设置背景透明 setBackgroundDrawable(new ColorDrawable(0)); } } }
使用:在自己的MainActivity调用showTopRightPopMenu()。
/** * 显示右上角popup菜单 */ private void showTopRightPopMenu() { /** * 用屏幕宽高计算popwindow的宽高 */ WindowManager wm = this.getWindowManager(); int width = wm.getDefaultDisplay().getWidth(); int height = wm.getDefaultDisplay().getHeight(); int popWidth = width / 2; int popHeight = height / 3; if (topPopWindow == null) { //(activity,onclicklistener,width,height) topPopWindow = new TopPopWindow(MainActivity.this, this, popWidth, popHeight); //监听窗口的焦点事件,点击窗口外面则取消显示 topPopWindow.getContentView().setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (!hasFocus) { topPopWindow.dismiss(); } } }); //pop消失时 背景调至原来的亮度 topPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { backgroundAlpaha(MainActivity.this, 1.0f); } }); } //设置默认获取焦点 topPopWindow.setFocusable(true); //以某个控件的x和y的偏移量位置开始显示窗口 topPopWindow.showAsDropDown(bt_add, 0, 18); backgroundAlpaha(MainActivity.this, 0.5f); //如果窗口存在,则更新 topPopWindow.update(); } /** * 设置添加屏幕的背景透明度 1.popwindow弹窗用作背景变暗 **/ public void backgroundAlpaha(Activity context, float bgAlpha) { WindowManager.LayoutParams lp = context.getWindow().getAttributes(); lp.alpha = bgAlpha; context.getWindow() .addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); context.getWindow().setAttributes(lp); }
0 0
- 安卓仿微信菜单栏PopWindow
- POPwindow
- popwindow
- popwindow
- PopWindow
- popwindow
- popwindow
- PopWindow
- popwindow
- popwindow
- popwindow
- popwindow下拉菜单 显示位置问题 类似微信的菜单栏
- popwindow下拉菜单 显示位置问题 类似微信的菜单栏
- Android开发问题:底部弹出popwindow,会被某些手机底部的菜单栏挡住
- 菜单栏
- 菜单栏
- 菜单栏
- 菜单栏
- 欢迎使用CSDN-markdown编辑器
- 蛙宝任务检索
- Hihocoder 147周 小HI的烦恼 bitset(技巧)
- 通俗理解条件熵
- wav文件格式分析
- 安卓仿微信菜单栏PopWindow
- 2017浙江省选二试第一天游记
- 神经网络基础
- HTTPS POST持续修改
- swustoj俄式乘法(0642)
- Linux学习第一章_003
- js实现图片淡入淡出
- Android Studio常用快捷键总结
- Console类:控制台输入输出