Ошибка несоответствия темы android alerttdialog - PullRequest
0 голосов
/ 14 февраля 2019

У меня есть диалоговое окно с предупреждением, которое появляется, когда вы нажимаете кнопку в моем приложении.Код довольно прост и его можно увидеть ниже:

final String[] options = {"Bar Graph", "Pie Chart", "Text Summary"};

AlertDialog.Builder builder = new AlertDialog.Builder(myActivity.this);
builder.setTitle("Choose a summary");
builder.setIcon(R.drawable.summaryicon);
builder.setItems(options,  ... );

См. Изображение ниже, как это выглядит.Это хорошо.

enter image description here

Однако, как ни странно, иногда, когда я строю свое приложение на эмуляторе, тема диалогового окна предупреждения меняется и выглядит следующим образом:

enter image description here

Я не могу представить, что вызвало бы это случайное изменение, казалось бы, непредсказуемым образом.

Я пытался использовать эту строку, чтобы установить тему вручную, но, похоже, это не дает никакого эффекта.

ContextThemeWrapper themedContext = new ContextThemeWrapper( AnalysisActivity.this, android.R.style.ThemeOverlay_Material_Dialog_Alert );

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

Я запутался и ищу способ установить тему вручную или обеспечитьоно не меняется по умолчанию.

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

Примечание: эта альтернативная тема выглядит как старая тема, так что, возможно, есть проблема с версией?

Ответы [ 2 ]

0 голосов
/ 16 февраля 2019

Наряду с ответом Динеша Сармы, код, который я сейчас внедрил для создания действия алертиалдиалога, можно увидеть здесь.

Он создает макет, который выглядит следующим образом:

enter image description here

Используйте этот код макета в файле с именем alert_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/background_light"
        android:orientation="horizontal"
        android:weightSum="12">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:layout_weight="7"
            android:contentDescription="Icon"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:scaleType="fitCenter"
            app:srcCompat="@drawable/summaryicon" />

        <TextView
            android:id="@+id/textView13"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="5"
            android:gravity="center_vertical"
            android:text="Choose a Summary"
            android:textSize="18sp"
            android:textStyle="normal" />
    </LinearLayout>

    <Button
        android:id="@+id/bar_graph"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:background="@android:color/background_light"
        android:gravity="center_vertical"
        android:paddingStart="20dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:text="Pie Chart"
        android:textAllCaps="false"
        android:typeface="sans" />

    <Button
        android:id="@+id/pie_chart"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:background="@android:color/background_light"
        android:gravity="center_vertical"
        android:paddingStart="20dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:text="Bar Graph"
        android:textAllCaps="false"
        android:typeface="sans" />

    <Button
        android:id="@+id/text_summary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:background="@android:color/background_light"
        android:gravity="center_vertical"
        android:paddingStart="20dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:text="Text Summary"
        android:textAllCaps="false"
        android:typeface="sans" />
</LinearLayout>
0 голосов
/ 14 февраля 2019

попробуйте этот код ... шаг-1: создайте файл макета вашего диалогового окна с предупреждением (этот макет является вашим разработанным alertDialog) шаг-2: и используйте этот код

public void displayAlertDialog() {
    LayoutInflater factory = LayoutInflater.from(this);
    final View alertDialogView = factory.inflate(R.layout.alert_dialog, null);
    final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setView(alertDialogView);

    alertDialogView.findViewById(R.id.bar_graph).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //your business logic
            alertDialog.dismiss();
        }
    });
    alertDialogView.findViewById(R.id.pie_chart).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //your business logic
            alertDialog.dismiss();
        }
    });
    alertDialogView.findViewById(R.id.text_summary).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //your business logic
            alertDialog.dismiss();
        }
    });

    alertDialog.show();
}
...