当前位置: 移动技术网 > IT编程>移动开发>Android > Android简单实现自定义弹框(PopupWindow)

Android简单实现自定义弹框(PopupWindow)

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

群论坛,汤姆克鲁斯 科学教,听见颜色的女孩读后感

一:一般都是先上效果图

二:实现步骤:

1.xml布局实现

<?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"
 android:background="@drawable/store_bgimg">
 <relativelayout
 android:layout_width="fill_parent"
 android:layout_height="214dp"
 android:layout_centervertical="true"
 android:layout_marginleft="31dp"
 android:layout_marginright="31dp"
 android:background="@drawable/tkbjzj">
 <textview
  android:id="@+id/tetle"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerhorizontal="true"
  android:layout_margintop="26dp"
  android:text="七天连锁酒店"
  android:textcolor="#262626"
  android:textsize="18dp" />
 <textview
  android:id="@+id/textdz"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@+id/tetle"
  android:layout_centerhorizontal="true"
  android:layout_margintop="34dp"
  android:text="你已领取本店7.5折优惠券"
  android:textcolor="#ea302e" />
 <view
  android:layout_width="match_parent"
  android:layout_height="0.5dp"
  android:layout_above="@+id/lineardb"
  android:background="#e6e6e6" />
 <linearlayout
  android:id="@+id/lineardb"
  android:layout_width="match_parent"
  android:layout_height="44dp"
  android:layout_alignparentbottom="true">
  <textview
  android:id="@+id/textwzdl"
  android:layout_width="0dp"
  android:layout_height="fill_parent"
  android:layout_weight="1"
  android:gravity="center"
  android:text="我知道了"
  android:textcolor="#262626"
  android:textsize="16dp" />
  <textview
  android:id="@+id/textckxq"
  android:layout_width="0dp"
  android:layout_height="fill_parent"
  android:layout_weight="1"
  android:background="#f86c6a"
  android:gravity="center"
  android:text="查看详情"
  android:textcolor="#ffffff"
  android:textsize="16dp" />
 </linearlayout>
 </relativelayout>
</relativelayout>

2.drawable文件下的转角,然后在布局引用

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
 <!-- 背景颜色 -->
 <solid android:color="#ffffff" />
 <!-- 控制边界线颜色和大小 -->
 <stroke
 android:width="1dp"
 android:color="#ffffff" />
 <!-- 控制圆角大小 -->
 <corners android:radius="4dp" />
</shape>

3.activity的实现

/**
 * 弹框
 */
private view mpopupheadviewy;//创建一个view
private popupwindow mheadpopupclly;//popupwindow
private textview tetle, textdz;//title,打折
private textview textwzdl, textckxq;//我知道了,查看详情
@suppresswarnings("deprecation")
private void popupheadwindowcll() {
 mpopupheadviewy = view.inflate(getactivity(), r.layout.tankuang_layout, null);
 tetle = (textview) mpopupheadviewy.findviewbyid(r.id.tetle);
 textdz = (textview) mpopupheadviewy.findviewbyid(r.id.textdz);
 textwzdl = (textview) mpopupheadviewy.findviewbyid(r.id.textwzdl);
 textckxq = (textview) mpopupheadviewy.findviewbyid(r.id.textckxq);
 mheadpopupclly = new popupwindow(mpopupheadviewy, abslistview.layoutparams.match_parent, abslistview.layoutparams.match_parent, true);
 // 在popupwindow里面就加上下面代码,让键盘弹出时,不会挡住pop窗口。
 mheadpopupclly.setinputmethodmode(popupwindow.input_method_needed);
 mheadpopupclly.setsoftinputmode(windowmanager.layoutparams.soft_input_adjust_resize);
 mheadpopupclly.setbackgrounddrawable(new bitmapdrawable());
 mheadpopupclly.setoutsidetouchable(true);
 mheadpopupclly.showasdropdown(textviewid, 0, 0);
 textwzdl.setonclicklistener(new view.onclicklistener() {
 @override
 public void onclick(view v) {
  mheadpopupclly.dismiss();
 }
 });
 textckxq.setonclicklistener(new view.onclicklistener() {
 @override
 public void onclick(view v) {
  mheadpopupclly.dismiss();
  toast.maketext(getactivity(), "查看详情", toast.length_long).show();
 }
 });
}

注意:

1、

mheadpopupclly = new popupwindow(mpopupheadviewy, abslistview.layoutparams.match_parent, abslistview.layoutparams.match_parent, true);

这句代码就是控制弹出框是铺满屏幕还是自适应

2、

mheadpopupclly.showasdropdown(textviewid, 0, 0);

这句话是这个弹框基于哪个控件之下,textviewid是控件名,后面两个是坐标

这是一个简单的自定义弹框,大神勿喷,有用的希望顶一下

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

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

相关文章:

验证码:
移动技术网