安卓用PopupWindow实现自定义Dailog

  

Android的PopupWindow是个很有用的小部件,利用它可以实现悬浮窗体的效果,比如实现一个悬浮的菜单,最常见的应用就是在视频播放界面里,做一个工具栏,用来控制播放进度。本文利用PopupWindow来实现一个通用的Dailog,类似Android系统的AlertDailog,从中学习和掌握有关PopupWindow和Dailog的使用和实现细节。

  

界面效果如图所示,点击点击按钮后,弹出对话框提示。

  

 Android用PopupWindow实现自定义Dailog”> <br/>
  </p>
  <p> <强>(1)只CustomDailog的布局</强> </p>
  <p>首先定义CustDailog的布局文件,由系统的AlertDailog可以知道,一个对话框包含了三个要素,一个是标题,即标题,一个是消息,即主体内容,还有一个是按钮,即确定和取消的按钮,用来与用户交互。因此,布局设计如下:</p>
  
  <pre类=   & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt; LinearLayout xmlns: android=" http://schemas.android.com/apk/res/android "   android: layout_width=" wrap_content "   android: layout_height=" wrap_content "   android:取向=按怪薄?   android:背景=" @drawable/shape_bg”   android: layout_margin=" 10 dp”比;      & lt; TextView   android: id=癅 + id/CustomDlgTitle”   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   android: textStyle=按蟮ā?   android: textSize=" 20 sp”   android: layout_margin=" 10 dp”   android:重力="中心"/比;      & lt;视图   android: layout_width=" match_parent "   android: layout_height=" 1 dp "   android:背景=" @android:颜色/darker_gray”/比;      LinearLayout & lt;   android: id=癅 + id/CustomDlgContentView”   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   android:取向=按怪薄?   android: layout_margin=" 5 dp/比;      & lt; TextView   android: id=癅 + id/CustomDlgContentText”   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   android: textSize=" 15 sp”   android: layout_margin=" 5 dp "   android: paddingLeft=" 5 sp”/比;      LinearLayout & lt;   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   面向android:="水平"   android: layout_margin=" 5 dp "比;      & lt;按钮   android: id=癅 + id/CustomDlgButtonOK”   android: layout_width=" 0 dp”   android: layout_weight=" 0.5 "   android: layout_height=" wrap_content "   android:可见性="了"/比;      & lt;按钮   android: id=癅 + id/CustomDlgButtonCancel”   android: layout_width=" 0 dp”   android: layout_weight=" 0.5 "   android: layout_height=" wrap_content "   android:可见性="了"/比;      & lt;/LinearLayout>      & lt;/LinearLayout>      之前      

其中,shap_bg。xml是Dailog的背景的定义文件,你可以修改此文件,来改变Dailog的背景:

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 " & # 63;比;   & lt;形状android:形状=熬匦巍?   xmlns: android=" http://schemas.android.com/apk/res/android "比;   & lt;固体android:颜色=" # e6ecee”/比;   & lt;中风android:宽度=" 1.0下降“android:颜色=癅android:颜色/darker_gray”/比;   & lt;角落android:=?.0浸”/半径比;   & lt;/shape>      之前      

<强> (2)。CustomDailog的定义

  

CustomDailog的接口,可以类比AlertDailg的接口定义,主要包括如下一些方法:

  

1只;已设置标题
  2只;setMessage设置主体内容
  3只;setPositiveButton设置”确定”按钮
  4只;setNegativeButton设置“取消”按钮
  5只;show ,显示
  6只;dimiss消失

  

其定义如下:

        包com.ticktick.popdailog;      进口android.content.Context;   进口android.view.Gravity;   进口android.view.LayoutInflater;   进口android.view.View;   进口android.view.View.OnClickListener;   进口android.view.ViewGroup.LayoutParams;   进口android.widget.Button;   进口android.widget.LinearLayout;   进口android.widget.PopupWindow;   进口android.widget.TextView;      公开课CustomDailog {      私人观点mParent;   私人PopupWindow mPopupWindow;   私人LinearLayout mRootLayout;   私人LayoutParams mLayoutParams;//PopupWindow必须有一个ParentView,所以必须添加这个参数   公共CustomDailog(上下文语境,视图父){      mParent=父母;      LayoutInflater mInflater=(LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);//加载布局文件   mRootLayout=(LinearLayout) mInflater.inflate(出来。custom_dailog, null);      mLayoutParams=new LayoutParams (LayoutParams.WRAP_CONTENT LayoutParams.WRAP_CONTENT);   }//设置Dailog的标题   公共空间setTitle(字符串标题){   TextView mTitle=(TextView) mRootLayout.findViewById (R.id.CustomDlgTitle);   mTitle.setText(标题);   }//设置Dailog的主体内容   公共空间setMessage(字符串消息){   TextView mMessage=(TextView) mRootLayout.findViewById (R.id.CustomDlgContentText);   mMessage.setText(消息);   }//设置Dailog的“确定”按钮   公共空间setPositiveButton(字符串文本、OnClickListener侦听器){   最后一个按钮buttonOK=(按钮)mRootLayout.findViewById (R.id.CustomDlgButtonOK);   buttonOK.setText(文本);   buttonOK.setOnClickListener(听众);   buttonOK.setVisibility (View.VISIBLE);   }//设置Dailog的“取消”按钮   公共空间setNegativeButton(字符串文本、OnClickListener侦听器){   最后一个按钮buttonCancel=(按钮)mRootLayout.findViewById (R.id.CustomDlgButtonCancel);   buttonCancel.setText(文本);   buttonCancel.setOnClickListener(听众);   buttonCancel.setVisibility (View.VISIBLE);   }//替换Dailog的“主体”布的局   公共空间setContentLayout(视图布局){      TextView mMessage=(TextView) mRootLayout.findViewById (R.id.CustomDlgContentText);   mMessage.setVisibility (View.GONE);      LinearLayout contentLayout=(LinearLayout) mRootLayout.findViewById (R.id.CustomDlgContentView);   contentLayout.addView(布局);   }//设置Dailog的长宽   公共空间setLayoutParams (int宽度,int高度){   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

安卓用PopupWindow实现自定义Dailog