gpt4 book ai didi

java - 如何为自定义对话框设置圆角?

转载 作者:行者123 更新时间:2023-12-01 14:10:14 25 4
gpt4 key购买 nike

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

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"

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

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

因为没有 getWindow() 函数。

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

最佳答案

您可以使用 Material Components 库中包含的 MaterialAlertDialogBu​​ilder:

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>

enter image description here

关于java - 如何为自定义对话框设置圆角?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62703501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com