前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android PopupWindow实现左侧弹窗效果

Android PopupWindow实现左侧弹窗效果

作者头像
砸漏
发布2020-10-26 10:38:47
1.3K0
发布2020-10-26 10:38:47
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

本文实例为大家分享了Android PopupWindow实现左侧弹窗的具体代码,供大家参考,具体内容如下

效果图:

MainActivity.java页面核心代码:

代码语言:javascript
复制
protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  //在setContentView之前添加,未添加的话home键监听无效,设置窗体属性
  this.getWindow().setFlags(0x80000000, 0x80000000);
  setContentView(R.layout.activity_main);

  //创建广播
  //InnerRecevier innerReceiver = new InnerRecevier();
  //动态注册广播
  //IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
  //启动广播
  //registerReceiver(innerReceiver, intentFilter);

  //外部网页
  // init();

  //pop
  Button pop = (Button) findViewById(R.id.popButton);
  pop.setOnClickListener(popClick);
 }
 View.OnClickListener popClick = new View.OnClickListener() {
  @Override
  public void onClick(View v) {
   getPopupWindow();
   popupWindow.showAtLocation(v, Gravity.LEFT,0,0);
  }
 };
 /*创建PopupWindow*/
 protected void initPopupWindow(){
  //获取自定义布局文件activity_pop_left.xml 布局文件
  final View popipWindow_view = getLayoutInflater().inflate(R.layout.activity_pop_left,null,false);
  //创建Popupwindow 实例,200,LayoutParams.MATCH_PARENT 分别是宽高
  popupWindow = new PopupWindow(popipWindow_view,300, ViewGroup.LayoutParams.MATCH_PARENT,true);
//设置动画效果
  popupWindow.setAnimationStyle(R.style.AnimationFade);
  //点击其他地方消失
  popipWindow_view.setOnTouchListener(new View.OnTouchListener() {
   @Override
   public boolean onTouch(View v, MotionEvent event) {
    if (popipWindow_view != null && popipWindow_view.isShown()) {
     popupWindow.dismiss();
     popupWindow = null;
    }
    return false;
   }
  });
  popupWindow.setBackgroundDrawable(new ColorDrawable(0));
  Button button1 = (Button) popipWindow_view.findViewById(R.id.button1);
  button1.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    Toast.makeText(getApplicationContext(),"全屏显示",Toast.LENGTH_SHORT).show();
   }
  });
 }
/*获取PopipWinsow实例*/

private void getPopupWindow(){
 if (null!=popupWindow){

  popupWindow.dismiss();
  return;
 }else {
  initPopupWindow();
 }
}

activity_main.xml页面

代码语言:javascript
复制
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/container"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.example.x.MainActivity"
 tools:ignore="MergeRootFrame"  

 <WebView 
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:id="@+id/webView"
 / 
 <Button
  android:id="@+id/popButton"
  android:text="点击弹出左菜单" android:layout_width="fill_parent"
  android:layout_height="wrap_content" / 
</FrameLayout 

左侧菜单需单独设置一个xml页面,style样式自定义。

以上就是本文的全部内容,希望对大家的学习有所帮助。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档