【问题标题】:How to set round corners for a custom Dialog?如何为自定义对话框设置圆角?
【发布时间】:2020-10-23 11:24:23
【问题描述】:

这是我的自定义对话框的代码:

public class DialogBrightness extends AppCompatDialogFragment {

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.layout_dialog, null);
    
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    /*Build and create the dialog here*/
    
    }
}

我按照其他答案的说明首先创建了这个名为 dialog_bg 的可绘制 xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid
            android:color="#fff5ee"/>
        <corners
            android:radius="30dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
</shape>

然后将其设置为layout_dialog xml的背景:

android:background="@drawable/dialog_bg"

但我无法完成将对话框的根视图设置为透明的最后一步:

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

因为没有 getWindow() 函数。

当他们说根视图时,他们是指我在上面的 inflate 函数中设置为 null 的那个吗?

【问题讨论】:

    标签: java android android-alertdialog android-dialogfragment material-components-android


    【解决方案1】:

    您可以使用 Material Components 库中包含的MaterialAlertDialogBuilder

    import androidx.fragment.app.DialogFragment;
    
    public class CustomDialog extends DialogFragment {
    
        //...
    
        @NonNull
        @Override
        public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
    
    
            return  new MaterialAlertDialogBuilder(getActivity(),R.style.MaterialAlertDialog_rounded)
                    .setTitle("Title")
                    .setMessage("Message")
                    .setPositiveButton("OK", null)
                    .create();
    
        }
    }
    

    与:

    <style name="MaterialAlertDialog_rounded" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
        <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MyApp.Dialog.Rounded</item>
    </style>
    
    <style name="ShapeAppearanceOverlay.MyApp.Dialog.Rounded" parent="">
        <item name="cornerFamily">rounded</item>
        <item name="cornerSize">8dp</item>
    </style>
    

    【讨论】:

    • 谢谢这个简单一点。
    【解决方案2】:

    我强烈推荐@GabrieleMariotti 的回答,但我在这里写信是为了看看您如何以原始方式实施。

    没有 getWindow() 函数。

    您可以使用 requireView() 代替:

    dialogBrightness.requireView().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));  
    

    当他们说根视图时,他们是指我在上面的 inflate 函数中设置为 null 的那个吗?

    是的,是的。一般不建议将null作为root参数传递,因为需要root视图来计算view当前正在被LayoutInflater膨胀的布局参数。相反,您应该像这样使用它:

    View view = LayoutInflater.from(requireContext()).inflate(R.layout.layout_dialog, rootView, false);  
    

    这里,第三个参数是attachToRoot,它被传递为false

    【讨论】:

    • 究竟什么是rootView?
    • rootViewViewGroup 类型的对象(它扩展了View 类并且是布局和视图容器的基类)。如果 attachToRoot 为 false,rootView用于计算布局参数,否则当前膨胀的view 将是rootView
    猜你喜欢
    • 1970-01-01
    • 2011-09-03
    • 2013-05-16
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    相关资源
    最近更新 更多