Using a Material-based Dialog Theme with AppCompat

会有一股神秘感。 提交于 2020-01-11 15:37:07

问题


I have an activity in my Manifest I used to style with a Dialog Theme. I can not find how to replace this in AppCompat library.

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

Is there a Material-based equivalent?


回答1:


There is no material based theme for a dialog in AppCompat yet, see here

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

Response

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

Update:

In version 22.1 of the Support Library you can now get the material dialog style by using AppCompatDialog




回答2:


Java code

    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();

Use this theme

  <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>

Import support v7 alert dialog

import android.support.v7.app.AlertDialog;

Output like this,




回答3:


Use the latest Appcompat library

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

and in Manifest use the following theme

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



回答4:


This should work for you: https://github.com/afollestad/material-dialogs

I used it to build the dialog in a DialogFragment, with custom styles applied. Works great.



来源:https://stackoverflow.com/questions/26554600/using-a-material-based-dialog-theme-with-appcompat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!