Возможно ли, что Dialog может работать с сочетанием закругленных углов и темой DayNight одновременно в Android? - PullRequest
1 голос
/ 20 сентября 2019

ТЕКУЩЕЕ ОБНОВЛЕНИЕ: Все еще не решено!

Вопрос: Возможно ли, что Dialog / AlertDialog / AlertDialogBuilder / MaterialAlaertDialogBuilder может работать с комбинацией округленныхуглы и тема DayNight одновременно в Android?

Я очень устал, но не думаю, что это возможно.Будет работать только одна вещь.

Вы можете предложить мне использовать библиотеку материалов.

В Диалоге действительно возможен закругленный угол со стилем темы?

Код:

public void showFeedbackDialog(int code) {
        exitCode = code;
        dialog = new Dialog(context, R.style.dialogBoxStyle);

        View dialogView = LayoutInflater.from(context).inflate(R.layout.row_feedback_dialog, null);
        name = dialogView.findViewById(R.id.feedback_name);
        email = dialogView.findViewById(R.id.feedback_email);
        content = dialogView.findViewById(R.id.feedback_content);
        TextView nameError = dialogView.findViewById(R.id.nameError);
        emailError = dialogView.findViewById(R.id.emailError);
        feedbackError = dialogView.findViewById(R.id.feedbackError);
        progressBar = dialogView.findViewById(R.id.progress_bar);
        skip = dialogView.findViewById(R.id.skip);
        submit = dialogView.findViewById(R.id.button_positive);
        cancel = dialogView.findViewById(R.id.button_negative);

        dialog.setContentView(dialogView);

        if (code == 101) {
            skip.setVisibility(View.VISIBLE);
        } else if (code == 102) {
            skip.setVisibility(View.GONE);
        }
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                submit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (checkValidation()) {
                            addFeedbackData();
                        }
                    }
                });

                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                skip.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (code == 101) {
                            FeedbackDialog.this.dialog.dismiss();
                            FixedCategorySingleton.getInstance().setNullObject();
                            activity.finishAffinity();
                        }
                    }
                });
            }
        });


        dialog.setCancelable(false);
        dialog.show();
        dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

    }

row_feedback_dialog

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:focusableInTouchMode="true"
    android:background="@drawable/dialog_positive_round"
    app:cardCornerRadius="12dp"
    android:orientation="vertical">

    <include
        layout="@layout/row_progress_bar"
        android:visibility="gone" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dp">


        <TextView
            style="@style/dialog_hint_textview_style"
            android:text="Please share your Feedback"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                style="@style/hint_textview_style"
                android:text="@string/name" />
            <EditText
                android:id="@+id/feedback_name"
                style="@style/edit_text_style"
                android:singleLine="true"
                android:imeOptions="actionNext" />

        </LinearLayout>
        <include
            android:id="@+id/nameError"
            layout="@layout/row_error_textview" />

       <!-- <EditText
            android:id="@+id/feedback_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/colorIconLightGray"
            android:hint="Name (Optional)"
            android:inputType="textPersonName"
            android:textColor="?attr/textcolor" />-->



        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                style="@style/hint_textview_style"
                android:text="@string/email" />
            <EditText
                android:id="@+id/feedback_email"
                style="@style/edit_text_style"
                android:singleLine="true"
                android:imeOptions="actionNext" />

        </LinearLayout>
        <include
            android:id="@+id/emailError"
            layout="@layout/row_error_textview" />

       <!-- <EditText
            android:id="@+id/feedback_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/colorIconLightGray"
            android:hint="Email (Optional)"
            android:inputType="textEmailAddress"
            android:textColor="?attr/textcolor" />-->


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                style="@style/hint_textview_style"
                android:text="What would you like to tell us?" />
            <EditText
                android:id="@+id/feedback_content"
                style="@style/edit_text_style"
                android:maxLines="4"
                android:imeOptions="actionDone" />
        </LinearLayout>


       <!-- <EditText
            android:id="@+id/feedback_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/colorIconLightGray"
            android:gravity="top"
            android:hint="What would you like to tell us?"
            android:imeOptions="actionDone"
            android:inputType="textMultiLine"
            android:lines="5"
            android:textColor="?attr/textcolor" />-->

        <include
            android:id="@+id/feedbackError"
            layout="@layout/row_error_textview" />

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?attr/backgroundcolor">


            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <Button
                        android:id="@+id/skip"
                        android:layout_width="70dp"
                        android:layout_height="40dp"
                        android:background="?attr/backgroundcolor"
                        android:text="@string/skip_caps"
                        android:textColor="?attr/textcolor"
                        android:visibility="gone" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:orientation="horizontal">

                    <Button
                        android:id="@+id/button_negative"
                        android:layout_width="70dp"
                        android:layout_height="40dp"
                        android:background="?attr/backgroundcolor"
                        android:text="@string/cancel"
                        android:textColor="?attr/textcolor" />

                    <Button
                        android:id="@+id/button_positive"
                        android:layout_width="70dp"
                        android:layout_height="40dp"
                        android:background="?attr/backgroundcolor"
                        android:text="@string/submit"
                        android:textColor="?attr/textcolor" />
                </LinearLayout>


            </RelativeLayout>
        </RelativeLayout>
    </LinearLayout>

</androidx.cardview.widget.CardView>

style

  <style name="dialogBoxStyle" parent="Theme.AppCompat.DayNight.Dialog.Alert">
        <item name="android:background">?attr/backgroundcolor</item>
        <item name="android:textColor">?attr/textcolor</item>
        <item name="android:textColorAlertDialogListItem">?attr/textcolor</item>
        <item name="android:textColorSecondary">?attr/textcolor</item>
        <item name="android:textColorPrimary">?attr/textcolor</item>
        <item name="colorAccent">?attr/textcolor</item>
        <item name="android:typeface">normal</item>
        <item name="textColorAlertDialogListItem">?attr/textcolor</item>
    </style>

Если я не использую тему, товсе будет работать нормально.угол идет в округлой форме из-за линии getWindow.Но в моем приложении я тоже использую тему.

Я прошу вас помочь мне, я наконец-то создал эту учетную запись, потому что мне нужно решение.

ОБНОВЛЕНИЕ:

пробовал с библиотекой материалов, без эффекта

1 Ответ

0 голосов
/ 20 сентября 2019

Вы можете использовать MaterialAlertDialogBuilder, включенные в библиотеку Компоненты материала .
. Используйте метод .setView(R.layout.dialog_layout), чтобы надуть ваш пользовательский макет.

Что-то вроде:

 new MaterialAlertDialogBuilder(MainActivity.this,
            R.style.MyThemeOverlay_MaterialComponents_MaterialAlertDialog)
              .setTitle("Dialog")
              .setView(R.layout.dialog_layout)
              .setPositiveButton("Ok", null)
              .setNegativeButton("Skip", null)
              .show();

Затем используйте shapeAppearanceOverlay, чтобы определить свою форму и применить закругленные углы (требуется v.1.1.0).

 <!-- Alert Dialog -->
  <style name="MyThemeOverlay.MaterialComponents.MaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MyApp.Dialog.Rounded</item>
    <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
    ...
  </style>


  <style name="PositiveButtonStyle" parent="@style/Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="android:textColor">#FFFFFF</item>
    <item name="backgroundTint">#00f</item>
  </style>

  <!-- Rounded corners -->
  <style name="ShapeAppearanceOverlay.MyApp.Dialog.Rounded" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">16dp</item>
  </style>

enter image description here

Также вы можете использовать атрибут materialThemeOverlay для переопределения стиля / цвета по умолчаниюопределено в теме вашего приложения, не меняя его для всех компонентов.

Что-то вроде:

<style name="MyThemeOverlay.MaterialComponents.MaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="materialThemeOverlay">@style/MyAlThemeOverlay</item>
    ...
</style>

  <style name="MyAlThemeOverlay" parent="ThemeOverlay.MaterialComponents.TextInputEditText.FilledBox">
    <item name="colorPrimary">@color/....</item>
  </style>

Также библиотека Material Components поддерживает тему DayNight .
Тема вашего приложения просто наследуется от Theme.MaterialComponents.DayNight.

Что-то вроде:

<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
    <!-- ... -->
</style>

или:

res/values/themes.xml:

<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light">
    <!-- ...  -->
</style>

res/values-night/themes.xml:

<style name="Theme.MyApp" parent="Theme.MaterialComponents">
    <!-- ... -->
</style>

Без изменения кода AlertDialog наследуется от темы приложения.

...