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
    评论
可以通过设置DialogFragment的样式来实现底部半屏幕的效果。具体做法如下: 1.在styles.xml文件中添加样式: ```xml <style name="BottomDialog" parent="Theme.AppCompat.Light.Dialog"> <item name="android:windowIsFloating">false</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowAnimationStyle">@style/BottomDialogAnimation</item> <item name="android:windowSoftInputMode">stateAlwaysHidden|adjustResize</item> <item name="android:backgroundDimEnabled">true</item> </style> <style name="BottomDialogAnimation"> <item name="android:windowEnterAnimation">@anim/dialog_slide_in_bottom</item> <item name="android:windowExitAnimation">@anim/dialog_slide_out_bottom</item> </style> ``` 其中,android:windowIsFloating设置为false,使DialogFragment全屏幕显示;android:windowBackground设置为透明,以便在DialogFragment周围创建一个半透明的背景;android:windowAnimationStyle设置为自定义的BottomDialogAnimation样式,用来设置DialogFragment的进入和退出动画。 2.在anim目录下添加dialog_slide_in_bottom.xml和dialog_slide_out_bottom.xml动画文件。这里我们使用了valueAnimator来实现动画效果。 dialog_slide_in_bottom.xml文件内容如下: ```xml <set xmlns:android="http://schemas.android.com/apk/res/android"> <objectAnimator android:propertyName="translationY" android:valueFrom="800" android:valueTo="0" android:duration="500" android:interpolator="@android:interpolator/decelerate_cubic" /> </set> ``` dialog_slide_out_bottom.xml文件内容如下: ```xml <set xmlns:android="http://schemas.android.com/apk/res/android"> <objectAnimator android:propertyName="translationY" android:valueFrom="0" android:valueTo="800" android:duration="500" android:interpolator="@android:interpolator/decelerate_cubic" /> </set> ``` 3.在DialogFragment的onCreateView方法中设置样式: ```java @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_dialog, container, false); //设置样式 if (getDialog() != null) { getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); getDialog().getWindow().setWindowAnimations(R.style.BottomDialog); } return view; } ``` 其中,getDialog().getWindow().setWindowAnimations设置为我们刚刚定义的BottomDialog样式,用来设置进入和退出动画。 这样就可以实现一个底部半屏幕的DialogFragment了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值