DialogFragment style设置

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);//背景黑暗度














  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值