Android Using a Material-based Dialog Theme with AppCompat

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26554600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:47:58  来源:igfitidea点击:

Using a Material-based Dialog Theme with AppCompat

androidthemesandroid-appcompatmaterial-designmaterial-theme

提问by TheLettuceMaster

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

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

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

Is there a Material-based equivalent?

回答by Ranjith Kumar

Java code

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

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 support v7 alert dialog

import android.support.v7.app.AlertDialog;

Output like this,

Output like this,

enter image description here

enter image description here

回答by tyczj

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

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?

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

Response

Response

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

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

Update:

Update:

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

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

回答by Saleem Khan

Use the latest Appcompat library

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

and in Manifest use the following theme

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

回答by benhylau

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

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.

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