Android Dialog 设置圆角无效

这两天有一个需求:设置dialog圆角,写完后发现并没有达到效果,以前也碰到这个问题,这里记录下解决方案,便于查阅。也有百度去查询原因,却没有发现合适的解答,当然更可能是我没找到,还是自己解决吧。

Dialog与DialogFragment 解决方案一致:只要设置背景透明解决问题了。

 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

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

        setContentView(view);
        setCancelable(true);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //设置背景透明
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        View view = View.inflate(getActivity(), R.layout.dialog_zntchain, null);
        return view;
    }

看到这里,不难明白,应该是google自己设置了默认的背景,虽然我们设置了view为圆角,但是在挂在已有背景下,我们就是自然看不到,看下源码,google也有说道透明问题。

 

 

 /**
     * Change the background of this window to a Drawable resource. Setting the
     * background to null will make the window be opaque. To make the window
     * transparent, you can use an empty drawable (for instance a ColorDrawable
     * with the color 0 or the system drawable android:drawable/empty.)
     *
     * @param resId The resource identifier of a drawable resource which will
     *              be installed as the new background.
     */
    public void setBackgroundDrawableResource(@DrawableRes int resId) {
        setBackgroundDrawable(mContext.getDrawable(resId));
    }

 

 

 

 

 


 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要在Android设置Dialog圆角,可以使用以下步骤: 1. 创建一个自定义的Dialog样式。在res/values/styles.xml文件中,定义一个新的样式如下: ```xml <style name="CustomDialog" parent="@android:style/Theme.Dialog"> <item name="android:windowBackground">@drawable/dialog_background</item> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowFullscreen">false</item> <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item> </style> ``` 2. 创建一个自定义的对话框背景。在res/drawable文件夹中,创建一个名为dialog_background.xml的文件,并添加以下内容: ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FFFFFF" /> <corners android:radius="10dp" /> </shape> ``` 在上面的代码中,可以根据需要调整`android:radius`属性的值来设置圆角的大小。 3. 创建一个AlertDialog实例并应用自定义样式。在代码中创建AlertDialog时,使用之前定义的自定义样式即可: ```java AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.CustomDialog); builder.setTitle("Custom Dialog"); builder.setMessage("This is a custom dialog with rounded corners."); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 点击确定按钮的操作 } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 点击取消按钮的操作 } }); AlertDialog dialog = builder.create(); dialog.show(); ``` 通过以上步骤,你可以在Android设置Dialog圆角样式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值