加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

DialogFragment 实现圆角效果

发布时间:2020-12-16 06:06:59 所属栏目:百科 来源:网络整理
导读:以前使用AlertDialog或者Dialog的时候使用一张xml 圆角的drawable文件作为 dialog的背景即可实现圆角效果。 但是在使用DialogFragment的时候发现 无论如何设置 drawable 文件中 corners属性的radius值,都没有效果。 后来在stackoverflow上面发现了一个解决

以前使用AlertDialog或者Dialog的时候使用一张xml 圆角的drawable文件作为 dialog的背景即可实现圆角效果。

但是在使用DialogFragment的时候发现 无论如何设置 drawable 文件中 corners属性的radius值,都没有效果。

后来在stackoverflow上面发现了一个解决办法。

首先
1. 在DialogFragment的布局文件设置的时候 留出一些padding空间,
2. 然后在fragment中oncreateView方法加上一句代码。设置window的背景为透明色。

getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

具体代码如下

这是布局文件

<?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中内部实现的代码

@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本身是方角的,会覆盖已经设置好的圆角效果。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读