如何为自定义对话框设置圆角?
How to set round corners for a 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*/
}
}
我按照其他答案的说明首先创建了这个名为 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() 函数。
此外,当他们说根视图时,他们是指我在上面的膨胀函数中设置为 null 的视图吗?
您可以使用 Material 组件库中包含的 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>
我强烈推荐@GabrieleMariotti 的回答,但我在这里写信是为了看看您如何以原来的方式实施。
there's no getWindow() function.
您可以使用 requireView()
作为 :
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?
是的,是的。通常,不建议将 null
作为 root
参数传递,因为需要 root
视图来计算 view
的布局参数,该 view
当前正被 LayoutInflater
。相反,你应该像这样使用它:
View view = LayoutInflater.from(requireContext()).inflate(R.layout.layout_dialog, rootView, false);
这里,第三个参数是attachToRoot
,作为false
传递。
这是我的自定义对话框的代码:
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() 函数。
此外,当他们说根视图时,他们是指我在上面的膨胀函数中设置为 null 的视图吗?
您可以使用 Material 组件库中包含的 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>
我强烈推荐@GabrieleMariotti 的回答,但我在这里写信是为了看看您如何以原来的方式实施。
there's no getWindow() function.
您可以使用 requireView()
作为 :
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?
是的,是的。通常,不建议将 null
作为 root
参数传递,因为需要 root
视图来计算 view
的布局参数,该 view
当前正被 LayoutInflater
。相反,你应该像这样使用它:
View view = LayoutInflater.from(requireContext()).inflate(R.layout.layout_dialog, rootView, false);
这里,第三个参数是attachToRoot
,作为false
传递。