使用PopoPWindow当做AlertDialog,控制Dialog的背景大小
来源:互联网 发布:淘宝的管控记录是什么 编辑:程序博客网 时间:2024/05/22 10:55
一开始在项目中用到弹出的AlertDialog,发现需求中的AlertDialog的背景透明且是左边不显示,右边显示出来。自己想这用Dialog可以实现吧!但是尝试了很久没有实现出来。自己决定用PopupWindow,最后解决了。使用AlertDialog没有实现也许是自己的技术不行吧!
1.自定义的PopupWindow
public class MyPopupWindow extends PopupWindow {
private TextView mTvP_Number;private View mView; public MyPopupWindow(){ super(); } public MyPopupWindow(Context context,AttributeSet attr){ super(context,attr); } public MyPopupWindow(final Context context){ setWidth(LayoutParams.MATCH_PARENT); setHeight(LayoutParams.MATCH_PARENT); setFocusable(true); setTouchable(true); setOutsideTouchable(false); setBackgroundDrawable(new BitmapDrawable()); setTouchInterceptor(new OnTouchListener() @Overridepublic boolean onTouch(View view, MotionEvent event) {if(event.getAction()==MotionEvent.ACTION_OUTSIDE){dismiss();return true;}return false;}}); } public void addView(View mview){ this.mView=mview; setContentView(this.mView); } public void setMessage(String msg,int id){ mTvP_Number=(TextView)this.mView.findViewById(id); mTvP_Number.setText(msg); } public void setSpanned(Spanned msg,int id){ mTvP_Number=(TextView)this.mView.findViewById(id); mTvP_Number.setText(msg); }}首先解释下为什么这样自定义MyPopupWindow?是为了适应不同的xml布局,这样只要在MainActivity中获得View view=LayoutInflater.from(this).inflater(xxx,xxx);直接把view传到 addView(View view)方法中就可以了,非常方便!setTouchInterceptor()方法中if()内容是为了再点击非PopupWindow区域会触发。
2.下面是一个PopupWindow中的布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/wbg"
android:gravity="center_vertical|center_horizontal">
<LinearLayout
android:layout_width="350dp"
android:layout_height="250dp"
android:orientation="vertical"
android:background="@drawable/dialog_shape"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取号成功!"
android:background="@color/transparent"
android:layout_marginTop="40dp"
android:textColor="#EEEEEEEE"
android:textSize="30sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:textSize="20sp"
android:background="@color/transparent"
android:textColor="#EEEEEEEE"
android:text="请告知客户票号为:"/>
<TextView
android:id="@+id/tvp_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="R05"
android:textSize="30sp"
android:background="@color/transparent"
android:textColor="#EE950C"/>
</LinearLayout>
</LinearLayout>
下面是values文件下的colors.xml中的
<color name="br">#979797</color>
<color name="wbg">#40404040</color>
这个布局文件就不解释了
3.最后在mainActivity中调用PopupWindow显示的位置
myPopupWindow.showAtLocation(myPopupView,Gravity.CLIP_VERTICAL|Gravity.CLIP_HORIZONTAL,420,0);这段代码可以控制背景显示的位置!
最后希望这个小小的案例可以帮到大家!我也是一个小菜鸟!
- 使用PopoPWindow当做AlertDialog,控制Dialog的背景大小
- android AlertDialog Dialog显示位置、大小,背景
- Dialog大小控制和Dialog中使用的ListView自适应
- 控制Dialog 的背景方法
- Dialog的使用(一):用AlertDialog提示
- Dialog的使用(二):AlertDialog.setItems
- 控制自定义Dialog的大小和位置
- Dialog 大小和位置的控制
- 控制Dialog的窗口大小(自定义Dialog视图)
- 控制dialog 的大小按屏幕大小的百分比设置
- 自定义Dialog,背景,大小,位置。
- Android之PopopWindow、Dialog监听返回键
- 关于AlertDialog的背景
- alertDialog的背景自定义
- android开发-使用Dialog AlertDialog
- android开发-使用Dialog AlertDialog
- android开发-使用Dialog AlertDialog
- Android 例子: Dialog的使用--- AlertDialog功能演示一
- 解决MyEclipse不能自动提示代码的功能
- JSP隐式对象到servlet API的映射
- 1598 find the most comfortable road
- zigbee中profile,cluster的认识
- How to install Hadoop 2.2.0
- 使用PopoPWindow当做AlertDialog,控制Dialog的背景大小
- Think Python 学习笔记5:条件与递归
- Huffman Codes
- Swift入门教程12-可选链
- ubuntu eclipse 启动图标是个问号
- 将最小值放到数列最前边
- cin.get()
- 各种文件的十六进制文件头
- 关于VSOnDraw找不到 ,加载文字或者String