【问题标题】:Is there a material component for Simple Dialog?Simple Dialog 有材质组件吗?
【发布时间】:2020-02-08 02:41:10
【问题描述】:

here 所述,在不同类型的对话框下,列出了简单对话框。是否有一个对话生成器会自动生成这样的对话?它只是带有自定义主题的警报对话框吗? 我想复制一个类似于 Android 10 中使用的新权限对话框的对话框。 我尝试使用材料警报对话框生成器:

new MaterialAlertDialogBuilder(context)
            .setTitle("Title")
            .setMessage("Message")
            .setPositiveButton("Ok", null)
            .setNegativeButton("Ok", null)
            .show();

但这会水平显示选项,而我希望垂直显示它们。 我的应用中有许多自定义对话框,因此我可以根据需要创建另一个对话框,但我想更好地了解可以与材质组件一起使用的内容。

出于这个原因,我从 github 下载了material-components-android 项目,在那里我找到了列出不同类型对话框的 DialogMainDemoFragment。更接近我想要的那个是这样创建的:

addDialogLauncher(
        dialogLaunchersLayout,
        R.string.long_title_message_too_long_actions,
        new MaterialAlertDialogBuilder(getContext())
            .setTitle(getResources().getString(R.string.long_title))
            .setMessage(message)
            .setPositiveButton(getResources().getString(R.string.too_long_positive), null)
            .setNegativeButton(getResources().getString(R.string.too_long_negative), null)
            .setNeutralButton(getResources().getString(R.string.too_long_neutral), null));

在哪里

private void addDialogLauncher(
      ViewGroup viewGroup, @StringRes int stringResId, AlertDialog.Builder alertDialogBuilder) {
    MaterialButton dialogLauncherButton = new MaterialButton(viewGroup.getContext());
    dialogLauncherButton.setOnClickListener(v -> alertDialogBuilder.show());
    dialogLauncherButton.setText(stringResId);
    viewGroup.addView(dialogLauncherButton);
  }

但我看不到样式/主题的确定位置。

【问题讨论】:

标签: android material-design android-alertdialog material-components material-components-android


【解决方案1】:

您可以覆盖材料主题的值。 如果我没记错的话,你确实需要这些样式

    <style name="MaterialAlertDialog.MaterialComponents.Title.Panel.CenterStacked" parent="Base.MaterialAlertDialog.MaterialComponents.Title.Panel">
       <item name="android:orientation">vertical</item>
    </style>

【讨论】:

  • 感谢您的建议。我必须添加更多属性才能获得所需的效果,但至少您的答案可以垂直显示选项!
猜你喜欢
  • 1970-01-01
  • 2022-08-07
  • 2018-12-16
  • 1970-01-01
  • 2020-08-06
  • 2018-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多