广告
返回顶部
首页 > 资讯 > 移动开发 >Android简单实现自定义弹框(PopupWindow)
  • 445
分享到

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

自定义popupwindowAndroid 2022-06-06 01:06:54 445人浏览 安东尼
摘要

一:一般都是先上效果图 二:实现步骤: 1.xml布局实现 <?xml version="1.0" encoding="utf-8"?> &

一:一般都是先上效果图

二:实现步骤:

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是控件名,后面两个是坐标

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

您可能感兴趣的文章:android PopupWindow 和 Activity弹出窗口实现方式android popwindow实现左侧弹出菜单层及PopupWindow主要方法介绍Android Animation实战之屏幕底部弹出PopupWindowAndroid编程实现popupwindow弹出后屏幕背景变成半透明效果Android入门之PopupWindow用法实例解析Android实现底部弹出PopupWindow背景逐渐变暗效果Android之用PopupWindow实现弹出菜单的方法详解android自定义popupwindow仿微信右上角弹出菜单效果Android中自定义PopupWindow实现弹出框并带有动画效果Android PopupWindow实现右侧、左侧和底部弹出菜单Android开发之PopupWindow创建弹窗、对话框的方法详解


--结束END--

本文标题: Android简单实现自定义弹框(PopupWindow)

本文链接: https://www.lsjlt.com/news/21801.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作