How to set round corners for a custom Dialog?

折月煮酒 提交于 2021-02-04 06:16:21

问题


This is code for my custom Dialog:

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*/
    
    }
}

I followed instructions from other answers by first creating this drawable xml called dialog_bg:

<?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>

Then setting it as the background of the layout_dialog xml:

android:background="@drawable/dialog_bg"

But I can't do the final step which is to set the dialog's root view to transparent:

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

because there's no getWindow() function.

Also when they say root view do they mean the one that I set to null in the inflate function above?


回答1:


You can use the MaterialAlertDialogBuilder included in the Material Components library:

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();

    }
}

with:

<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:


I highly recommend @GabrieleMariotti's answer but I am writing here to see how you could have implemented in your original way.

there's no getWindow() function.

You could have used requireView() instead as :

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

Also when they say root view do they mean the one that I set to null in the inflate function above?

Yes, it is. Generally, it is not recommended to pass null as a root parameter as root view is needed to calculate the layout parameters of the view that is currently being inflated by the LayoutInflater. Instead, you should use it like this :

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

Here, 3rd parameter is attachToRoot which is passed as false.



来源:https://stackoverflow.com/questions/62703501/how-to-set-round-corners-for-a-custom-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!