- class MDialogFragment extends DialogFragment{
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //1 通过样式定义
- setStyle(DialogFragment.STYLE_NORMAL,R.style.Mdialog);
- //2代码设置 无标题 无边框
- //setStyle(DialogFragment.STYLE_NO_TITLE|DialogFragment.STYLE_NO_FRAME,0);
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- //3 在此处设置 无标题 对话框背景色
- //getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
- // //对话框背景色
- //getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.RED));
- //getDialog().getWindow().setDimAmount(0.5f);//背景黑暗度
- //不能在此处设置style
- // setStyle(DialogFragment.STYLE_NORMAL,R.style.Mdialog);//在此处设置主题样式不起作用
- return inflater.inflate(R.layout.l_dialog_fragment,container,false);
- }
- @Override
- public void onStart() {
- getDialog().getWindow().getAttributes().width=getResources().getDisplayMetrics().widthPixels;
- getDialog().getWindow().setGravity(Gravity.BOTTOM);//对齐方式
- super.onStart();
- }
- }
1.通过样式文件定义DialogFragment 的样式
在onCreate 中设置:
- setStyle(DialogFragment.STYLE_NORMAL,R.style.Mdialog);
注意只能在此处设置,在onCreateView中设置style ,不会起作用
style定义如下:
- <style name="Mdialog" parent="android:Theme.Holo.Light.Dialog">
- <item name="android:windowBackground"> @drawable/bg</item>
- <item name="android:windowFrame">@null</item>
- <item name="android:backgroundDimEnabled">true</item>
- <item name="android:windowIsTranslucent">true</item>
- <item name="android:windowNoTitle">true</item>
- <item name="android:windowFullscreen">true</item>
- <item name="android:windowContentOverlay">@null</item>
- </style>
- <drawable name="bg">#ff3058ff</drawable>
2.也可以直接在代码中设置 无标题 无边框,以及背景色
2.1onCreate中:
- //2代码设置 无标题 无边框
- //setStyle(DialogFragment.STYLE_NO_TITLE|DialogFragment.STYLE_NO_FRAME,0);
2.2或者在onCreateView中:
- //3 在此处设置 无标题 对话框背景色
- //getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
- // //对话框背景色 原有边框会自动消失
- //getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.RED));
- //getDialog().getWindow().setDimAmount(0.5f);//背景黑暗度