当前位置: 移动技术网 > IT编程>移动开发>Android > Android使用popUpWindow带遮罩层的弹出框

Android使用popUpWindow带遮罩层的弹出框

2019年08月01日  | 移动技术网IT编程  | 我要评论

万州区委书记徐海荣,食神之路130831,太原诺基亚专卖

上次项目中实现了新功能,就一直想添加到博客里来着,惰性发作起来简直太可怕,不说了,跟着一起写吧,三步即可实现简单的弹出框功能,首先看效果——

首先:主页面布局,触发控件一定要有,再有就是给根标签设置id

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/layout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.example.android_popupwindow.mainactivity" >
  <scrollview
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:scrollbars="none">
    <relativelayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">
      <imageview
        android:id="@+id/p"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scaletype="centercrop"
        android:src="@drawable/p"/>
      <imageview
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/p"
        android:scaletype="centercrop"
        android:src="@drawable/p"/>
   <button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click me" 
        android:background="#fff"
        android:padding="10dip"/>
    </relativelayout>
  </scrollview>
</relativelayout>

第二步:弹出框样式设置

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  tools:context="com.example.adf.mainactivity" >
  <textview
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="交友需带三分侠气,做人要存一点素心\n —《菜根谭》"
    android:textcolor="#000"
    android:background="@drawable/layout_border" />
</linearlayout>

最后:就是主代码了

public class mainactivity extends activity {
 private relativelayout layout;
 private button btn;
 private boolean isfold=true; // 判断是否显示
 private popupwindow taxwindow; // 弹出框
 private textview tv=null; // 遮罩层
 
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    layout=(relativelayout)findviewbyid(r.id.layout);
    btn=(button)findviewbyid(r.id.btn);
    btn.setonclicklistener(new view.onclicklistener(){
     @override
     public void onclick(view v){
     if(isfold){
      isfold=false;
       <span style="white-space:pre"> </span>showtaxdetail(v);
      tv=new textview(mainactivity.this);
       <span style="white-space:pre"> </span>tv.setlayoutparams(new viewgroup.layoutparams(viewgroup.layoutparams.fill_parent,viewgroup.layoutparams.fill_parent));
       <span style="white-space:pre"> </span>tv.setbackgroundcolor(color.parsecolor("#66000000"));
       <span style="white-space:pre"> </span>tv.setclickable(true);
       <span style="white-space:pre"> </span>tv.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view v) {
   isfold=true;
       taxwindow.dismiss();
       layout.removeview(tv);
   }
  });
       <span style="white-space:pre"> </span>layout.addview(tv);
     }
     else{
      isfold=true;
      taxwindow.dismiss();
      layout.removeview(tv);
     }
     }
    });
  }
  
  private void showtaxdetail(view view){
   layoutinflater inflater=layoutinflater.from(this);
   // 加载弹出框的布局
   view contentview=inflater.inflate(r.layout.ewj_tax_detail, null);
   contentview.measure(0,0);
   taxwindow=new popupwindow(contentview,contentview.getmeasuredwidth(),contentview.getmeasuredheight(),true);
   //taxwindow.setbackgrounddrawable(getresources().getdrawable(r.drawable.ic_launcher));
   //taxwindow.setoutsidetouchable(true);
   taxwindow.setfocusable(false);
   int[] location = new int[2]; 
   // 得到按钮控件的坐标,便于定位弹出框位置
    btn.getlocationinwindow(location);
    int taxwindowwidth=taxwindow.getcontentview().getmeasuredwidth();
    int screenwidth = getwindowmanager().getdefaultdisplay().getwidth(); 
    taxwindow.showatlocation(btn,gravity.no_gravity,(screenwidth-taxwindowwidth)/2,location[1]+95);
  }
}

弹出框的位置在触发控件下方居中,如果有明确的横纵坐标,可以用下面的来实现

taxwindow.showasdropdown(anchor, xoffset, yoffset);

好了,这样就实现了。

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网