自定义dialog并且去除黑边
来源:互联网 发布:笔记本分区软件 编辑:程序博客网 时间:2024/05/01 16:20
今天尝试了一下自定义dialog。
想做成上传头像的那种,有两个button,一个作为手机摄像头拍摄用,一个调用手机SD卡的图库。
先是布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#11ffffff" android:layout_gravity="center" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="0dp" android:gravity="center" android:layout_weight="1" android:text="选择头像" android:textSize="18sp"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@drawable/cursor_color" /> <Button android:id="@+id/dialog_camera_btn" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:text="拍摄头像" android:textSize="16sp" android:background="@color/colorPrimaryText"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@drawable/cursor_color" /> <Button android:id="@+id/dialog_pic_btn" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:text="从图库中选择" android:textSize="16sp" android:background="@color/colorPrimaryText"/> </LinearLayout>LinearLayout的background是自定义的一个圆角,就不贴代码了。然后在代码里引入自定义dialog
View view = getLayoutInflater().inflate(R.layout.my_dialog,null); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyDialog); builder.setView(view); AlertDialog alertDialog = builder.create(); Button camera = view.findViewById(R.id.dialog_camera_btn); camera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, CAMERA_REQUEST_CODE); } }); alertDialog.setCanceledOnTouchOutside(true); //设置弹出框失去焦点是否隐藏,即点击屏蔽其它地方是否隐藏 alertDialog.show();
拿到的view就是上面自定义的dialog布局文件。因为我是在fragment里实例化一个AlertDialog,所以传入的Context是通过getActivity()拿到的。然后通过setView(view)加载布局。
中间拿到了其中一个button的实例,并且设置了点击监听事件。
最后别忘了show出来。
如果想加上自定义style的话,就在实例化AlertDialog.Builder里传入style即可(因为默认的确实丑)
我的style是这样的,网上有很多:
<!--自定义dialog背景全透明无边框theme --> <style name="MyDialog" parent="@android:style/Theme.DeviceDefault.Light.Dialog"> <!--背景颜色及和透明程度--> <item name="android:windowBackground">@android:color/transparent</item> <!--是否去除标题 --> <item name="android:windowNoTitle">true</item> <!--是否去除边框--> <item name="android:windowFrame">@null</item> <!--是否浮现在activity之上--> <item name="android:windowIsFloating">true</item> <!--是否模糊--> <item name="android:backgroundDimEnabled">false</item> </style>
这里有两个地方需要注意,一个是parent的属性值,网上有很多都是android:style/Theme.Dialog。如果parent的属性是这个的话,创建的dialog会有很严重的黑边,想要去除黑边把parent的属性值改为代码里的那个就好。还有一个windowBackgrount属性,如注释,可以自己定义一个颜色。最好是透明的。
最终的效果如下:
比较小,而且看起来很奇怪,有点像是嵌入到fragment里了。这个在布局和style改成自己想要的就行。
P.S:在自定义dialog布局那里,我加了两个横线,那里的View的V千万不要小写,否则在拿到dialog实例时会报空指针。
阅读全文
0 0
- 自定义dialog并且去除黑边
- android 自定义Dialog去除黑边
- 去除自定义alertdialog(dialog)黑边
- 最简单自定义dialog去除dialog黑边方法
- android_自定义dialog去除黑边_快捷方法
- 【Android】自定义透明dialog 去除边缘阴影 黑边
- 去除dialog的黑边
- 去除自定义AlertDialog黑边
- [经验之谈]自定义的Dialog教你怎么去除黑边框
- AlertDialog去除黑边自定义布局
- 笔记107--去除自定义AlertDialog黑边
- android 自定义Dialog去除标题栏
- 自定义Dialog如何去除阴影
- Android dialog去除边框代码 & 自定义dialog
- 带输入框的自定义dialog 点击外部消失 输入框可以弹出软键盘 去除外部黑框
- 自定义Dialog设置样式(去除白边)
- 去掉dialog的黑边
- Android自定义Dialog去除黑色边框
- python一些事(重启与清屏)
- CSS3
- 用python实现的一个猜数字游戏,
- (二)多线程之间实现同步
- block,inline和inline-block概念和区别
- 自定义dialog并且去除黑边
- MyBatis学习笔记1_上手
- 使用Docker 镜像
- JAVA 多线程实现简单UDP一对一聊天
- 一个治愈JavaScript疲劳的学习计划
- 【教程】html+css零基础入门教程之属性选择器详解(二十八)
- 一个月薪12000的北京程序员的真实生活
- Java垃圾收集基础
- hibernate一对一映射(注解)