关于AlertDialog.Builder犯的一个错和自定义布局的限制宽高一个知识点
来源:互联网 发布:免费远程培训软件 编辑:程序博客网 时间:2024/06/17 16:53
先看错误代码
AlertDialog.Builder builder=new AlertDialog.Builder(getApplicationContext()); builder.setTitle("温馨提示") .setMessage("确定要离开吗?") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "哈哈", Toast.LENGTH_SHORT).show(); } }).setNegativeButton("取消", null); builder.show();
乍一看,没错误啊,,,,可是运行起来会崩掉,,. 后来一行一行改代码调试了一下,发现了错误在这里
AlertDialog.Builder builder=new AlertDialog.Builder(this);//改为this 或者MainActivity.this 都是可以的
错误的原因: AlertDialog对象是依赖一个View的 而这个View的生命周期是和当前页面的Activity的生命周期保持一致的,绑定的是当前的Activity ,,,如果传入的是getApplicationContext().则是和整个应用的生命周期保持一致,所以运行起来就会崩掉
关于下面的一个包涉及到Dialog 宽高的问题
compile 'com.android.support:appcompat-v7:25.1.0'
之前用23版本的包时候,高度不会拉伸很长,可是换了这个版本之后,高度会被拉伸的很长,,,所以自定义Dialog的布局 搞的我很头疼
后来发现 自定义dialog中 : 按照下面的设置可以将dialog所依赖的窗口大小限制一下
Window window = dialog.getWindow();WindowManager.LayoutParams attributes = window.getAttributes();attributes.width=dip2px(200);//dip2px()是dp转px的方法attributes.height=dip2px(200);//下面是自己项目中用到的自定义Dialog所用到的代码先看图片![项目中要求做的效果](http://img.blog.csdn.net/20170110125349820?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhvdXhpbjEyMzM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
dialog用到的背景样式
<style name="customDialog" parent="@android:style/Theme.Dialog"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> </style>
dialog用到的布局文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RelativeLayout android:id="@+id/dialog_priceType" android:background="@android:color/white" android:layout_width="300dp" android:layout_height="350dp"> <LinearLayout android:id="@+id/linear_priceType" android:layout_marginTop="20dp" android:paddingLeft="20dp" android:paddingRight="20dp" android:orientation="horizontal" android:gravity="center_vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <View android:layout_weight="2" android:layout_marginRight="20dp" android:background="@android:color/darker_gray" android:layout_width="wrap_content" android:layout_height="0.6dp"/> <TextView android:text="舒适型" android:textColor="#484845" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <View android:layout_weight="2" android:layout_marginLeft="20dp" android:background="@android:color/darker_gray" android:layout_width="wrap_content" android:layout_height="0.6dp"/> </LinearLayout> <RelativeLayout android:id="@+id/rl1_priceType" android:layout_below="@+id/linear_priceType" android:paddingTop="20dp" android:paddingLeft="20dp" android:paddingRight="20dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="套餐价" android:textColor="#484845" android:layout_alignParentLeft="true" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView tools:text="353元" android:textColor="#484845" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout> <RelativeLayout android:id="@+id/rl2_priceType" android:layout_below="@+id/rl1_priceType" android:padding="20dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="包含里程" android:textColor="#484845" android:layout_alignParentLeft="true" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView tools:text="70公里" android:textColor="#484845" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout> <RelativeLayout android:id="@+id/rl3_priceType" android:layout_below="@+id/rl2_priceType" android:paddingLeft="20dp" android:paddingRight="20dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:textColor="#484845" android:text="超公里费(70公里以上)" android:layout_alignParentLeft="true" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:textColor="#484845" tools:text="3.8元/公里" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout> <RelativeLayout android:id="@+id/rl4_priceType" android:layout_below="@+id/rl3_priceType" android:padding="20dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:textColor="#484845" android:text="夜间费(23:00 - 05:00)" android:layout_alignParentLeft="true" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:textColor="#484845" tools:text="70公里" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout> <TextView android:paddingLeft="20dp" android:text="最多乘坐4人" android:layout_below="@id/rl4_priceType" android:textColor="@android:color/darker_gray" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:text="计价规则" android:textColor="#484845" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="20dp" android:drawableRight="@mipmap/help" android:drawablePadding="5dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout> <ImageButton android:id="@+id/iv_cancel" android:layout_below="@+id/dialog_priceType" android:src="@mipmap/cancel" android:background="@android:color/transparent" android:layout_marginTop="20dp" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content"/></RelativeLayout>
自定义CustomDialog类
public class CustomDialog extends Dialog { int layoutRes;//布局文件 Context context; public CustomDialog(Context context) { super(context); this.context = context; } /** * 自定义布局的构造方法 * @param context * @param resLayout */ public CustomDialog(Context context, int resLayout){ super(context); this.context = context; this.layoutRes=resLayout; } /** * 自定义主题及布局的构造方法 * @param context * @param theme * @param resLayout */ public CustomDialog(Context context, int theme,int resLayout){ super(context, theme); this.context = context; this.layoutRes=resLayout; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mView = View.inflate(getContext(), layoutRes, null); this.setContentView(mView); this.setCancelable(false); } private View mView; public View getView(){ return mView; }}
引用
final CustomDialog dialog=new CustomDialog(this, R.style.customDialog, R.layout.dialog_pricetype); dialog.show(); View view = dialog.getView(); ImageButton ivCel=(ImageButton)view.findViewById(R.id.iv_cancel); ivCel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dialog.dismiss(); } });
大功搞成
主要是下面的叉叉按钮 让我头疼了一下
0 0
- 关于AlertDialog.Builder犯的一个错和自定义布局的限制宽高一个知识点
- 关于AlertDialog.Builder犯的一个错和自定义布局的限制宽高一个知识点
- 关于AlertDialog.Builder的自定义
- AlertDialog.Builder和AlertDialog之间的关系
- Android详细的对话框AlertDialog.Builder使用方法和自定义样式
- 关于顾问容易犯的一个毛病
- AlertDialog使用自定义的布局
- 关于if的一个知识点
- 关于Android AlertDialog.builder的用法
- 关于AlertDialog.Builder的传参
- 一个页面布局的知识点总结
- android 自定义布局继承一个系统的布局会报错
- 关于AlertDialog的自定义样式
- 分享一个圆角自定义的漂亮AlertDialog
- 分享一个圆角自定义的漂亮AlertDialog
- Android 对话框AlertDialog和AlertDialog.Builder两者的区别
- AlertDialog.Builder的使用
- AlertDialog.Builder的setCancelable
- Android图片打水印
- 二进制安装MySQL 5.7.17 重置root密码
- break使用不当引发的一个“血案”
- 在Ubuntu16.10下使用Live Wallpaper
- 第一天
- 关于AlertDialog.Builder犯的一个错和自定义布局的限制宽高一个知识点
- 路由器刷第三方固件,宽带免费提速
- 在C++中用虚函数的作用是什么?为什么要用到虚函数
- Spring中的依赖检查
- Python 中 strip函数
- android异常总结--java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast t
- C++学习笔记一(对象和类)
- 几个递归小题
- 和我一起学Hadoop(三):Hadoop集群的最简化部署