欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义PopupWindow仿点击弹出分享功能

程序员文章站 2023-12-18 14:38:46
本文实例自定义popupwindow,点击弹出popupwindow,背景变暗,仿点击弹出分享功能,供大家参考,具体内容如下 注:参照大神代码写的 自定义代码...

本文实例自定义popupwindow,点击弹出popupwindow,背景变暗,仿点击弹出分享功能,供大家参考,具体内容如下

注:参照大神代码写的

Android自定义PopupWindow仿点击弹出分享功能

自定义代码

package com.duanlian.popupwindowdemo; 
 
import android.app.activity; 
import android.content.context; 
import android.graphics.drawable.colordrawable; 
import android.view.layoutinflater; 
import android.view.view; 
import android.view.windowmanager; 
import android.widget.linearlayout; 
import android.widget.popupwindow; 
import android.widget.textview; 
 
/** 
 * 自定义popupwindow 
 */ 
 
public class rewritepopwindow extends popupwindow { 
  private view mview; 
 
  public rewritepopwindow(activity context, view.onclicklistener itemsonclick) { 
    super(context); 
    initview(context, itemsonclick); 
  } 
 
  private void initview(final activity context, view.onclicklistener itemsonclick) { 
    layoutinflater minflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); 
    mview = minflater.inflate(r.layout.popupwindow_share, null); 
    linearlayout weixfriend = (linearlayout) mview.findviewbyid(r.id.weixinghaoyou); 
    linearlayout friendster = (linearlayout) mview.findviewbyid(r.id.pengyouquan); 
    linearlayout qqfriend = (linearlayout) mview.findviewbyid(r.id.qqhaoyou); 
    linearlayout qqzone = (linearlayout) mview.findviewbyid(r.id.qqkongjian); 
    textview canaletv = (textview) mview.findviewbyid(r.id.share_cancle); 
    canaletv.setonclicklistener(new view.onclicklistener() { 
      @override 
      public void onclick(view v) { 
 
        //销毁弹出框 
        dismiss(); 
        backgroundalpha(context, 1f); 
      } 
    }); 
    //设置按钮监听 
    weixfriend.setonclicklistener(itemsonclick); 
    friendster.setonclicklistener(itemsonclick); 
    qqfriend.setonclicklistener(itemsonclick); 
    qqzone.setonclicklistener(itemsonclick); 
    //设置selectpicpopupwindow的view 
    this.setcontentview(mview); 
    //设置selectpicpopupwindow弹出窗体的宽 
    this.setwidth(windowmanager.layoutparams.fill_parent); 
    //设置selectpicpopupwindow弹出窗体的高 
    this.setheight(windowmanager.layoutparams.wrap_content); 
    //设置selectpicpopupwindow弹出窗体可点击 
    this.setfocusable(true); 
    //设置popupwindow可触摸 
    this.settouchable(true); 
    //设置非popupwindow区域是否可触摸 
//    this.setoutsidetouchable(false); 
    //设置selectpicpopupwindow弹出窗体动画效果 
//    this.setanimationstyle(r.style.select_anim); 
    //实例化一个colordrawable颜色为半透明 
    colordrawable dw = new colordrawable(0x00000000); 
    //设置selectpicpopupwindow弹出窗体的背景 
    this.setbackgrounddrawable(dw); 
    backgroundalpha(context, 0.5f);//0.0-1.0 
    this.setondismisslistener(new ondismisslistener() { 
 
      @override 
      public void ondismiss() { 
        // todo auto-generated method stub 
        backgroundalpha(context, 1f); 
      } 
    }); 
  } 
 
 
  /** 
   * 设置添加屏幕的背景透明度 
   * 
   * @param bgalpha 
   */ 
  public void backgroundalpha(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); 
  } 
 
} 

mainactivity中用

package com.duanlian.popupwindowdemo; 
 
import android.os.bundle; 
import android.support.v7.app.appcompatactivity; 
import android.view.gravity; 
import android.view.view; 
import android.widget.toast; 
 
public class mainactivity extends appcompatactivity { 
  private rewritepopwindow mpopwindow; 
 
  @override 
  protected void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.activity_main); 
    findviewbyid(r.id.share).setonclicklistener(new view.onclicklistener() { 
      @override 
      public void onclick(view view) { 
        mpopwindow = new rewritepopwindow(mainactivity.this, itemsonclick); 
        mpopwindow.showatlocation(view, 
            gravity.bottom | gravity.center_horizontal, 0, 0); 
      } 
    }); 
  } 
 
  //为弹出窗口实现监听类 
  private view.onclicklistener itemsonclick = new view.onclicklistener() { 
 
    public void onclick(view v) { 
      mpopwindow.dismiss(); 
      mpopwindow.backgroundalpha(mainactivity.this, 1f); 
      switch (v.getid()) { 
        case r.id.weixinghaoyou: 
          toast.maketext(mainactivity.this, "微信好友", toast.length_short).show(); 
          break; 
        case r.id.pengyouquan: 
          toast.maketext(mainactivity.this, "朋友圈", toast.length_short).show(); 
          break; 
        case r.id.qqhaoyou: 
          toast.maketext(mainactivity.this, "qq好友", toast.length_short).show(); 
          break; 
        case r.id.qqkongjian: 
          toast.maketext(mainactivity.this, "qq空间", toast.length_short).show(); 
          break; 
        default: 
          break; 
      } 
    } 
 
  }; 
} 

布局文件--popupwindow布局

<?xml version="1.0" encoding="utf-8"?> 
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
 
       android:layout_height="match_parent" 
       > 
  <textview 
    android:layout_width="match_parent" 
    android:alpha="0.2" 
    android:layout_height="match_parent" 
    android:layout_above="@+id/ll"/> 
 
<linearlayout 
  android:id="@+id/ll" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:layout_alignparentbottom="true" 
  android:orientation="vertical"> 
  <linearlayout 
    android:paddingbottom="30dp" 
    android:id="@+id/share_ll" 
    android:paddingtop="20dp" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingright="20dp" 
    android:background="#ebebeb" 
    android:paddingleft="20dp" 
    android:orientation="horizontal" 
    > 
  <linearlayout 
    android:id="@+id/weixinghaoyou" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    > 
 
    <imageview 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:background="@mipmap/icon_weixin_fenxiang"/> 
 
    <textview 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:onclick="share" 
      android:layout_gravity="center" 
      android:layout_margintop="10dp" 
      android:text="微信好友" 
      android:textcolor="#000000"/> 
  </linearlayout> 
  <linearlayout 
    android:id="@+id/pengyouquan" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    > 
 
    <imageview 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:background="@mipmap/icon_quan"/> 
 
    <textview 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:onclick="share" 
      android:layout_gravity="center" 
      android:layout_margintop="10dp" 
      android:text="朋友圈" 
      android:textcolor="#000000"/> 
  </linearlayout> 
  <linearlayout 
    android:id="@+id/qqhaoyou" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    > 
 
    <imageview 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:background="@mipmap/icon_qq_fenxiang"/> 
 
    <textview 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:onclick="share" 
      android:layout_gravity="center" 
      android:layout_margintop="10dp" 
      android:text="qq" 
      android:textcolor="#000000"/> 
  </linearlayout> 
  <linearlayout 
    android:id="@+id/qqkongjian" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    > 
 
    <imageview 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:background="@mipmap/icon_kongjian"/> 
 
    <textview 
 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:onclick="share" 
      android:layout_gravity="center" 
      android:layout_margintop="10dp" 
      android:text="qq空间" 
      android:textcolor="#000000"/> 
  </linearlayout> 
  </linearlayout> 
 
  <textview 
    android:id="@+id/line" 
    android:layout_width="match_parent" 
    android:layout_height="0.5dp" 
    android:layout_below="@+id/share_ll" 
    android:background="#000000"/> 
<textview 
  android:id="@+id/share_cancle" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:gravity="center" 
  android:textsize="20sp" 
  android:layout_below="@+id/line" 
  android:background="#f5f3f3" 
  android:paddingbottom="20dp" 
  android:paddingtop="20dp" 
  android:text="取消"/> 
</linearlayout> 
</relativelayout> 

mainactivity的布局文件

<?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="com.duanlian.popupwindowdemo.mainactivity"> 
 
  <button 
    android:id="@+id/share" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="点击弹出"/> 
</relativelayout> 

demo下载地址:android popupwindow弹出分享

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

上一篇:

下一篇: