在AppCompat中使用基于材质的对话框主题

The*_*ter 21 android themes android-appcompat material-design material-theme

我在我的Manifest中有一个活动,我习惯用Dialog主题设置风格.我找不到如何在AppCompat库中替换它.

  <activity
            android:name=".LoginActivity"
            android:theme="@android:styles/Theme.Holo.Dialog" 
            android:configChanges="orientation|screenSize|keyboardHidden"
            android:label="Login" >
Run Code Online (Sandbox Code Playgroud)

有基于材料的等价物吗?

Ran*_*mar 58

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();
Run Code Online (Sandbox Code Playgroud)

使用这个主题

  <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>
Run Code Online (Sandbox Code Playgroud)

导入支持v7警报对话框

import android.support.v7.app.AlertDialog;
Run Code Online (Sandbox Code Playgroud)

像这样输出,

在此输入图像描述


tyc*_*czj 24

AppCompat中的对话框还没有基于材料的主题,请参见此处

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

响应

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

更新:

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


小智 9

使用最新的Appcompat库

compile 'com.android.support:appcompat-v7:23.2.1'// or any version greater than 22.1
Run Code Online (Sandbox Code Playgroud)

并在Manifest中使用以下主题

android:theme="@style/Theme.AppCompat.Light.Dialog"
Run Code Online (Sandbox Code Playgroud)