如何在Android应用中添加一个自定义弹框

如何在Android应用中添加一个自定义弹框?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

网站建设哪家好,找成都创新互联公司!专注于网页设计、网站建设、微信开发、小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了江都免费建站欢迎大家使用!

实现步骤:

1.xml布局实现

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<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文件下的转角,然后在布局引用

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<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应用中添加一个自定义弹框
文章出自:https://www.cdcxhl.com/article22/gcdccc.html

成都网站建设公司_创新互联,为您提供网站制作品牌网站建设域名注册静态网站企业网站制作

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

商城网站建设