Programming

머티리얼 디자인 스타일링 경고 대화 상자

procodes 2020. 6. 2. 21:55
반응형

머티리얼 디자인 스타일링 경고 대화 상자


appCompat 재질 디자인을 앱에 추가했는데 경고 대화 상자에서 기본, 기본 어두운 또는 강조 색을 사용하지 않는 것 같습니다.

내 기본 스타일은 다음과 같습니다.

<style name="MaterialNavyTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/apptheme_color</item>
    <item name="colorPrimaryDark">@color/apptheme_color_dark</item>
    <item name="colorAccent">@color/apptheme_color</item>
    <item name="android:textColorPrimary">@color/action_bar_gray</item>
</style>

내 이해를 바탕으로 대화 상자 버튼 텍스트 도이 색상을 사용해야합니다. 이해가 잘못 되었습니까? 아니면 더해야 할 일이 있습니까?


해결책:

표시된 답변이 올바른 길로 안내했습니다.

<style name="MaterialNavyTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/apptheme_color</item>
    <item name="colorPrimaryDark">@color/apptheme_color_dark</item>
    <item name="colorAccent">@color/apptheme_color</item>
    <item name="android:actionModeBackground">@color/apptheme_color_dark</item>
    <item name="android:textColorPrimary">@color/action_bar_gray</item>
    <item name="sdlDialogStyle">@style/DialogStyleLight</item>
    <item name="android:seekBarStyle">@style/SeekBarNavyTheme</item>
</style>

<style name="StyledDialog" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorPrimary">@color/apptheme_color</item>
    <item name="colorPrimaryDark">@color/apptheme_color_dark</item>
    <item name="colorAccent">@color/apptheme_color</item>
</style>

안드로이드 라이브러리의 머티리얼 컴포넌트로 2019 년 8 월에 업데이트 됨 :

Android 라이브러리 의 새로운 머티리얼 컴포넌트를 사용 com.google.android.material.dialog.MaterialAlertDialogBuilder하면 기존 androidx.appcompat.AlertDialog.Builder클래스 에서 확장되고 최신 머티리얼 디자인 사양을 지원 하는 새 클래스를 사용할 수 있습니다 .

다음과 같이 사용하십시오.

new MaterialAlertDialogBuilder(context)
            .setTitle("Dialog")
            .setMessage("Lorem ipsum dolor ....")
            .setPositiveButton("Ok", /* listener = */ null)
            .setNegativeButton("Cancel", /* listener = */ null)
            .show();

테마에서 다음과 같은 것을 사용할 수 있습니다.

 <!-- Base application theme. -->
 <style name="AppTheme" parent="Theme.MaterialComponents.Light">
    ...
    <item name="materialAlertDialogTheme">@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog</item>
 </style>

생성자를 사용할 수도 있습니다.

new MaterialAlertDialogBuilder(context, R.style.ThemeOverlay_MaterialComponents_MaterialAlertDialog)

지원 라이브러리 포함 :

새로운으로 AppCompat v22.1새 사용할 수 있습니다 android.support.v7.app.AlertDialog을 .

다음과 같은 코드를 사용하십시오.

import android.support.v7.app.AlertDialog

AlertDialog.Builder builder =
       new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
builder.setTitle("Dialog");
builder.setMessage("Lorem ipsum dolor ....");
builder.setPositiveButton("OK", null);
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>

그렇지 않으면 현재 테마에서 정의 할 수 있습니다.

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- your style -->
    <item name="alertDialogTheme">@style/AppCompatAlertDialogStyle</item>
</style>

그런 다음 코드에서 :

 import android.support.v7.app.AlertDialog

    AlertDialog.Builder builder =
           new AlertDialog.Builder(this);

Kitkat의 AlertDialog : enter image description here


대화 상자 작성기를 초기화 할 때 두 번째 매개 변수를 테마로 전달하십시오. API 레벨 21의 머티리얼 디자인이 자동으로 표시됩니다.

AlertDialog.Builder builder = new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_DARK);

또는,

AlertDialog.Builder builder = new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);

AppCompat은 대화 상자에서 그렇게하지 않습니다 (최소한은 아닙니다)

편집 : 이제 그렇습니다 . 사용하십시오android.support.v7.app.AlertDialog


이 프로젝트를 고려할 수 있습니다 : https://github.com/fengdai/AlertDialogPro

롤리팝과 거의 동일한 머티리얼 테마 경고 대화 상자를 제공 할 수 있습니다. 안드로이드 2.1과 호환됩니다.


당신은 사용할 수 있습니다

머티리얼 디자인 라이브러리

Material Design Library made for pretty alert dialogs, buttons, and other things like snack bars. Currently it's heavily developed.

Guide, code, example - https://github.com/navasmdc/MaterialDesignLibrary

Guide how to add library to Android Studio 1.0 - How do I import material design library to Android Studio?

.

Happy coding ;)


Try this library:

https://github.com/avast/android-styled-dialogs

It's based on DialogFragments instead of AlertDialogs (like the one from @afollestad). The main advantage: Dialogs don't dismiss after rotation and callbacks still work.


For some reason the android:textColor only seems to update the title color. You can change the message text color by using a

SpannableString.AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.MyDialogTheme));

AlertDialog dialog = builder.create();
                Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");
                wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                dialog.setMessage(wordtoSpan);
                dialog.show();

참고URL : https://stackoverflow.com/questions/26455919/material-design-not-styling-alert-dialogs

반응형