【问题标题】:Using a Material-based Dialog Theme with AppCompat将基于材质的对话框主题与 AppCompat 一起使用
【发布时间】:2014-12-20 16:27:48
【问题描述】:

我的清单中有一个活动,我曾经使用对话框主题设置样式。我在AppCompat 库中找不到如何替换它。

  <activity
            android:name=".LoginActivity"
            android:theme="@android:styles/Theme.Holo.Dialog" 
            android:configChanges="orientation|screenSize|keyboardHidden"
            android:label="Login" >

是否有基于材料的等价物?

【问题讨论】:

标签: android themes android-appcompat material-design material-theme


【解决方案1】:

Java 代码

    AlertDialog.Builder builder =
                    new AlertDialog.Builder(SecondActivity.this, R.style.AppCompatAlertDialogStyle);
            builder.setTitle("SCRUM");
            builder.setMessage("In the SCRUM methodology a sprint is the basic unit of development. Each sprint is preceded by a planning meeting, where the tasks for the sprint are identified and an estimated commitment for the sprint goal is made, and followed by a review or retrospective meeting where the progress is reviewed and lessons for the next sprint are identified. During each sprint, the team creates finished portions of a product.....");
            builder.setPositiveButton("OK", null);//second parameter used for onclicklistener
            builder.setNegativeButton("Cancel", null);
            builder.show();

使用这个主题

  <style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">#FFCC00</item>
    <item name="android:textColorPrimary">#FFFFFF</item>
    <item name="android:background">#5fa3d0</item>
</style>

导入支持 v7 警报对话框

import android.support.v7.app.AlertDialog;

这样的输出,

【讨论】:

  • 很好!!我在警报对话框中使用列表,似乎无法更改列表项的文本颜色?我尝试了文本颜色辅助和列表项,但没有效果!
  • 如果对话框扩展了 DialogFragment,如何处理错误“NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.NestedScrollView.setFocusable(boolean)' on an null object reference “?
  • 天哪!你太棒了gggggggggggg .. 过去 3 个小时你在哪里我试图找到你 :D .
  • 我使用 ANDROIDX,这对我不起作用。按钮变成了白色背景。我在使用 AndroidX 时发现的唯一解决方案是 @Rob 对这个问题的回答:stackoverflow.com/questions/52829954/…
【解决方案2】:

AppCompat 中的对话框还没有基于材质的主题,请参阅here

Will appcompat automatically theme dialogs to look like the Lollipop version?

回应

Not yet, but it's on the todo list.

更新:

Support Library 的版本22.1 中,您现在可以使用AppCompatDialog 获得材质对话框样式

【讨论】:

  • 想知道它是否会在 11 月 3 日正式发布之前发生
  • 由于谷歌还没有发布这样的东西,我为此做了一个库:github.com/AndroidDeveloperLB/MaterialStuffLibrary
  • @KickingLettuce 出于好奇,在哪里可以看到 AppCompat 的未来发布日期,也许还有关于发生了什么变化的注释?我找不到类似的东西。
  • @miav 这只是我在网上找到的一个谣言,我不认为它是 100% 准确的
  • 仅供参考:这是随 appcompat v22.1.0 发布的
【解决方案3】:

使用最新的 Appcompat 库

compile 'com.android.support:appcompat-v7:23.2.1'// or any version greater than 22.1

并在 Manifest 中使用以下主题

android:theme="@style/Theme.AppCompat.Light.Dialog"

【讨论】:

    【解决方案4】:

    这应该适合你:https://github.com/afollestad/material-dialogs

    我用它在DialogFragment 中构建对话框,并应用了自定义样式。效果很好。

    【讨论】:

    猜你喜欢
    • 2019-11-27
    • 1970-01-01
    • 2015-03-24
    • 1970-01-01
    • 1970-01-01
    • 2015-03-26
    • 2016-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多