【问题标题】:Material Design 3 DialogFragment is not styled like Dialogs using MaterialAlertDialogBuilderMaterial Design 3 DialogFragment 的样式不像使用 MaterialAlertDialogBu​​ilder 的对话框
【发布时间】:2022-11-09 23:02:22
【问题描述】:

我已迁移到 Material Design 3,并注意到 DialogFragments 的样式不像使用 MaterialAlertDialogBu​​ilder 创建的对话框。是否需要为 DialogFragments 添加自定义样式?我认为它应该开箱即用。我注意到 DialogFragments 没有圆角,并且表面颜色与您使用 MaterialAlertDialogBu​​ilder 创建的对话框不匹配。在需要自定义视图且无法使用 MaterialAlertDialogBu​​ilder 的情况下,我使用 DialogFragment。那么我将如何设置它看起来像 MaterialAlertDialog 的样式呢?

【问题讨论】:

  • 请添加显示差异的图像

标签: android material-ui material-components-android material-components


【解决方案1】:

在这里找到解决方案https://dev.to/bhullnatik/how-to-use-material-dialogs-with-dialogfragment-28i1

这是您需要覆盖 onCreateDialog 并使用标准 MaterialAlertDialogBu​​ilder 的示例代码

public class YourSexyMaterialDialogFragment extends DialogFragment {

  View theDialogView;

  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(requireActivity());
    theDialogView = onCreateView(LayoutInflater.from(requireContext()), null, savedInstanceState);
    builder.setView(theDialogView);

    return builder.create();
  }

  @Override
  public View getView() {
      return theDialogView;
  }

  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
  Bundle savedInstanceState) {
    // remains the same where you inflate your custom view and setup ui components
  }  

}

【讨论】:

    【解决方案2】:

    您有 2 个选项。

    • 使用getTheme()方法

    就像是:

    import androidx.fragment.app.DialogFragment
    
    class RoundedDialog: DialogFragment() {
    
        override fun getTheme() = R.style.RoundedCornersDialog
    
        //....
    
    }
    

    风格:

    <style name="RoundedCornersDialog" parent="Theme.Material3.DayNight.Dialog">
        <item name="dialogCornerRadius">16dp</item>
    </style>
    
    • onCreateDialog 方法中使用MaterialAlertDialogBuilder

    就像是:

    import androidx.fragment.app.DialogFragment
    import com.google.android.material.dialog.MaterialAlertDialogBuilder
    
    class RoundedAlertDialog : DialogFragment() {
    
        //...
    
        override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
            return MaterialAlertDialogBuilder(requireActivity(), R.style.App_Material3_MaterialAlertDialog)
                    .setTitle("Test")
                    .setMessage("Message")
                    .setPositiveButton("OK", null)
                    .create()
        }
    
    }
    

    风格:

    <style name="App.Material3.MaterialAlertDialog" parent="ThemeOverlay.Material3.MaterialAlertDialog">
        <item name="shapeAppearanceOverlay">@style/DialogCorners</item>
    </style>
    
    <style name="DialogCorners">
        <item name="cornerFamily">rounded</item>
        <item name="cornerSize">16dp</item>
    </style>
    

    【讨论】:

      猜你喜欢
      • 2021-05-14
      • 2014-12-14
      • 1970-01-01
      • 1970-01-01
      • 2018-12-24
      • 2017-01-21
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      相关资源
      最近更新 更多