DialogFragment 实现圆角效果
2015-09-25 20:21
337 查看
以前使用AlertDialog或者Dialog的时候使用一张xml 圆角的drawable文件作为 dialog的背景即可实现圆角效果。
但是在使用DialogFragment的时候发现 无论如何设置 drawable 文件中 corners属性的radius值,都没有效果。
后来在stackoverflow上面发现了一个解决办法。
首先
1. 在DialogFragment的布局文件设置的时候 留出一些padding空间,
2. 然后在fragment中oncreateView方法加上一句代码。设置window的背景为透明色。
具体代码如下
这是布局文件
这是dialogfragment中内部实现的代码
效果图
因为如果不设置 padding属性 TextView本身是方角的,会覆盖已经设置好的圆角效果。
但是在使用DialogFragment的时候发现 无论如何设置 drawable 文件中 corners属性的radius值,都没有效果。
后来在stackoverflow上面发现了一个解决办法。
首先
1. 在DialogFragment的布局文件设置的时候 留出一些padding空间,
2. 然后在fragment中oncreateView方法加上一句代码。设置window的背景为透明色。
[code]getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
具体代码如下
这是布局文件
[code]<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="200dp" android:layout_height="wrap_content" android:background="@drawable/exit_app_dialog_bg" android:orientation="vertical"> <LinearLayout android:paddingTop="5dp" android:paddingRight="5dp" android:paddingLeft="5dp" android:layout_width="match_parent" android:layout_height="65dp" android:layout_gravity="center" android:background="@drawable/exit_app_dialog_title_bg"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="确定退出应用?" android:textColor="@color/white" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/gray" /> <LinearLayout android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:layout_marginRight="5dp" android:layout_marginLeft="5dp" android:layout_width="match_parent" android:layout_height="45dp" android:orientation="horizontal"> <TextView android:id="@+id/btn_cancel" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="取消" android:textColor="@color/themeColor" /> <View android:layout_width="1px" android:layout_height="match_parent" android:background="@color/themeColor" /> <TextView android:id="@+id/btn_ok" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="#EEEEEE" android:gravity="center" android:text="确定" android:textColor="@color/themeColor" /> </LinearLayout> </LinearLayout>
这是dialogfragment中内部实现的代码
[code]@Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); View view = inflater.inflate(R.layout.dialog_exit_application,container,false); btn_ok = (TextView) view.findViewById(R.id.btn_ok); btn_cancel = (TextView) view.findViewById(R.id.btn_cancel); btn_ok.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { positiveListener.onPositiveClick(v); getDialog().dismiss(); } }); btn_cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { negativeListener.onNegativeClick(v); getDialog().dismiss(); } }); return view; }
效果图
因为如果不设置 padding属性 TextView本身是方角的,会覆盖已经设置好的圆角效果。
相关文章推荐
- 98 Validate Binary Search Tree
- ZOJ 3537 Cake (区间DP,三角形剖分)
- java Scanner类的几点注意事项
- ACM-ICPC ShangHai 2014
- html初始化
- 修改 this 指向
- 获取数组元素的个数
- TouchEvent、dispatchTouch、onInterceptTouchEvent之间的关系概述
- HDU 1075 What Are You Talking About(用map进行翻译)
- Objective-C 基础知识之 (十八):Copy NSString的Copy现象
- 《机器学习与R语言》读书笔记1:机器学习简介
- 把数组排成最小的数(剑指offer)string+vector
- 超越自我
- quick-union【连通图】
- NSURLSession和NSURLConnection比较
- C#编写NotifyIcon 将窗口最小化到托盘
- Objective-C 基础知识之 (十七):内存管理原则二
- 20135337——信息安全系统设计基础第二周学习总结
- UU-01-spi_nor_flash 研究1
- 讯飞语音万能五笔超强手写3合1输入法: