DialogFragment style设置

  1. class MDialogFragment extends DialogFragment{  
  2.         @Override  
  3.         public void onCreate(Bundle savedInstanceState) {  
  4.             super.onCreate(savedInstanceState);  
  5.             //1 通过样式定义  
  6.             setStyle(DialogFragment.STYLE_NORMAL,R.style.Mdialog);  
  7.             //2代码设置 无标题 无边框  
  8.             //setStyle(DialogFragment.STYLE_NO_TITLE|DialogFragment.STYLE_NO_FRAME,0);  
  9.   
  10.         }  
  11.   
  12.         @Override  
  13.         public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
  14.             //3 在此处设置 无标题 对话框背景色  
  15.             //getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);  
  16.             // //对话框背景色  
  17.             //getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.RED));  
  18.             //getDialog().getWindow().setDimAmount(0.5f);//背景黑暗度  
  19.   
  20.             //不能在此处设置style  
  21.             // setStyle(DialogFragment.STYLE_NORMAL,R.style.Mdialog);//在此处设置主题样式不起作用  
  22.             return inflater.inflate(R.layout.l_dialog_fragment,container,false);  
  23.         }  
  24.   
  25.         @Override  
  26.         public void onStart() {  
  27.   
  28.             getDialog().getWindow().getAttributes().width=getResources().getDisplayMetrics().widthPixels;  
  29.             getDialog().getWindow().setGravity(Gravity.BOTTOM);//对齐方式  
  30.             super.onStart();  
  31.         }  
  32.     }  


1.通过样式文件定义DialogFragment 的样式

在onCreate 中设置:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. setStyle(DialogFragment.STYLE_NORMAL,R.style.Mdialog);  

注意只能在此处设置,在onCreateView中设置style ,不会起作用

style定义如下:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <style name="Mdialog" parent="android:Theme.Holo.Light.Dialog">  
  2.     <item name="android:windowBackground"> @drawable/bg</item>  
  3.     <item name="android:windowFrame">@null</item>  
  4.     <item name="android:backgroundDimEnabled">true</item>  
  5.     <item name="android:windowIsTranslucent">true</item>  
  6.     <item name="android:windowNoTitle">true</item>  
  7.     <item name="android:windowFullscreen">true</item>  
  8.     <item name="android:windowContentOverlay">@null</item>  
  9. </style>  
  10.   
  11.   
  12. <drawable name="bg">#ff3058ff</drawable>  


2.也可以直接在代码中设置 无标题 无边框,以及背景色

2.1onCreate中:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //2代码设置 无标题 无边框  
  2.             //setStyle(DialogFragment.STYLE_NO_TITLE|DialogFragment.STYLE_NO_FRAME,0);  

2.2或者在onCreateView中:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //3 在此处设置 无标题 对话框背景色  
  2.            //getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);  
  3.            // //对话框背景色 原有边框会自动消失  
  4.            //getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.RED));  
  5.            //getDialog().getWindow().setDimAmount(0.5f);//背景黑暗度  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值