2017-11-16 3 views
2

Das ist meine dialogFragment Klasse unter:Fragment Dialog Animieren richtig, aber das Layout ist komplett schwarz

public static class MyDialog extends DialogFragment { 
     @SuppressLint("InflateParams") 
     @NonNull 
     @Override 
     public Dialog onCreateDialog(Bundle savedInstanceState) { 
      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.styleDialog); 
      // Get the layout inflater 
      LayoutInflater inflater = getActivity().getLayoutInflater(); 

      // Inflate and set the layout for the dialog 
      // Pass null as the parent view because its going in the dialog layout 
      builder.setView(inflater.inflate(R.layout.custom_dialog, null)); 

      return builder.create(); 
     } 

     @Override 
     public void onStart() { 
      super.onStart(); 

      if (getDialog() == null) { 
       return; 
      } 

      DisplayMetrics metrics = getContext().getResources().getDisplayMetrics(); 
      float dpValue = 170f; 
      float fPixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, metrics); 

      int dialogWidth = Math.round(fPixels); 
      int dialogHeight = ViewGroup.LayoutParams.WRAP_CONTENT; 

      if (getDialog().getWindow() != null) { 
       getDialog().getWindow().setLayout(dialogWidth, dialogHeight); 
      } 

     } 
    } 

Diese Methode aus meiner Tätigkeit genannt wird meinen Dialog zu zeigen:

private void showDialog() { 
    DialogFragment dialogFragment = new MyDialog(); 
    dialogFragment.show(getSupportFragmentManager(), MyDialog.class.getName()); 
} 

Das ist mein Stil. XML und anim.xml jeweils:

<style name="styleDialog" parent="@android:style/Theme.Dialog"> 
    <item name="android:windowAnimationStyle">@style/CustomDialogAnimation</item> 
</style> 

<style name="CustomDialogAnimation"> 
    <item name="android:windowEnterAnimation">@anim/animate_down_side_opens_up</item> 
    <item name="android:windowExitAnimation">@android:anim/slide_out_right</item> 
</style> 

<?xml version="1.0" encoding="utf-8"?> 
<scale xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="2000" 
    android:fromXScale="1" 
    android:fromYScale="0" 
    android:pivotX="50%" 
    android:pivotY="0%" 
    android:toXScale="1.0" 
    android:toYScale="1.0" /> 

AUSGANGSLAGE: Mein Dialog gleitet in einer offenen Erweiterung von oben nach unten und das ist genau das, was ich will.

PROBLEM: Der Dialog selbst ist jedoch vollständig schwarz und enthält keine der Ansichten.

enter image description here

Hinweis: Ohne jede Animation, wird der Dialog seine Layout-Ansicht Elemente enthalten.

EDIT: Hinzufügen von benutzerdefinierten Dialog Layout unter:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:weightSum="3"> 

<TextView 
    android:id="@+id/numberOneText" 
    android:layout_width="wrap_content" 
    android:layout_height="0dp" 
    android:layout_gravity="center" 
    android:layout_marginTop="5dp" 
    android:layout_weight="1" 
    android:gravity="center" 
    android:text="@string/numberOne" 
    android:textSize="16sp" /> 

<TextView 
    android:id="@+id/numberTwoText" 
    android:layout_width="wrap_content" 
    android:layout_height="0dp" 
    android:layout_gravity="center" 
    android:layout_marginTop="10dp" 
    android:layout_weight="1" 
    android:gravity="center" 
    android:text="@string/numberTwo" 
    android:textSize="16sp" /> 

<TextView 
    android:id="@+id/numberThreeText" 
    android:layout_width="wrap_content" 
    android:layout_height="0dp" 
    android:layout_gravity="center" 
    android:layout_marginTop="10dp" 
    android:layout_weight="1" 
    android:gravity="center" 
    android:text="@string/numberThree" 
    android:textSize="16sp" 
    android:layout_marginBottom="5dp"/> 

</LinearLayout> 

HINWEIS: Ändern der Klasse Alertdialog Import von android.support.v7.app.AlertDialog zu android.app.AlertDialog die Benutzeroberfläche geändert von das benutzerdefinierte Dialogfeld zu einem Dialogfeld mit doppeltem Quadrat. Bild unten angehängt:

enter image description here

+0

Irgendwie gelingt es mir nicht, den Fehler zu reproduzieren. Getestet mit zwei Emulatoren (Api Level 23 und 24) und einigen "Hello World" TextView in einem LinearLayout (du hast das Layout/custom_dialog.xml nicht mitgerechnet) – 0X0nosugar

+0

Ich werde weitermachen und es aufnehmen, obwohl es für dieses Szenario irgendwie irrelevant ist. –

+0

Ich würde zustimmen, dass es irrelevant ist, aber ich benutzte ein ConstraintLayout, das zufällig da war, bevor ich zum LinearLayout wechselte, das für mich funktionierte. Das ConstraintLayout wurde nicht angezeigt. Mein Hauptpunkt war jedoch, dass mit meinem Dummy-Layout die Animation funktionierte. Daher konnte ich den Fehler nicht reproduzieren. – 0X0nosugar

Antwort

0

es ist alles da ist, kann man einfach nicht sehen können. Ändern Sie Ihren Stil wie diese (eine Zeile hinzufügen, die erste innerhalb des ersten Style-Tag):

<style name="styleDialog" parent="@android:style/Theme.Dialog"> 
    <item name="android:background">@android:color/white</item> 
    <item name="android:windowAnimationStyle">@style/CustomDialogAnimation</item> 
</style> 

<style name="CustomDialogAnimation"> 
    <item name="android:windowEnterAnimation">@anim/anim</item> 
    <item name="android:windowExitAnimation">@android:anim/slide_out_right</item> 
</style> 

und Ihr Layout und der Inhalt sichtbar wird. Es sieht vielleicht nicht so aus, wie Sie es wollen, aber Sie können von dort aus arbeiten. Überzeugen Sie sich selbst:

enter image description here

Getestet mit android.support.v7.app.AlertDialog.

+0

Ich frage mich, wie du das entdeckt hast und wie ich das vermisst habe! : D Funktioniert großartig. –

+0

Schwarz war der Hintergrund und Schwarz ist die Standardfarbe des Textes, also habe ich einfach eine Standardfarbe versucht. – kalabalik