2017-09-15 3 views
0

Ich habe mein eigenes Layout für einen AlertDialog, und wenn ich SetPositiveButton verwenden, funktioniert alles. Aber wenn ich setItems verwende, wird mein Layout unterhalb der Elementschaltflächen angezeigt.AlertDialog Layout unter den Tasten

Wie kann ich mein benutzerdefiniertes Layout oben anzeigen?

Hier ist mein Code:

 private void selectImage(){ 
final CharSequence[] items = { TAKE_PICTURE, FROM_GALLERY, CANCLE}; 
      AlertDialog.Builder builder = new AlertDialog.Builder(Add_Object.this); 
      LayoutInflater inflater = this.getLayoutInflater(); 
      View content = inflater.inflate(R.layout.alert_dialog, null); 
      builder.setView(content); 
      ((TextView) content.findViewById(R.id.dialogTitle)).setText(R.string.addPictureTitle); 
      builder.setItems(items, new DialogInterface.OnClickListener(){ 
       @Override 
       public void onClick(DialogInterface dialog, int item) { 
        if (items[item].equals(TAKE_PICTURE)) { 
          captureImage(); 
        } else if (items[item].equals(FROM_GALLERY)) { 
          chooseFromGallery(); 
        } else if (items[item].equals(CANCLE)) { 
         dialog.dismiss(); 
        } 
       } 
      }); 
      builder.show(); 
     } 
    } 

Und mein Layout:

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <TextView 
     android:id="@+id/dialogTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="20dp" 
     android:layout_marginTop="15dp" 
     android:layout_marginBottom="15dp" 
     android:textSize="25sp" 
     android:textColor="@color/main_color"/> 

    <View 
     android:layout_width="match_parent" 
     android:id="@+id/dialogDivider" 
     android:layout_below="@id/dialogTitle" 
     android:layout_height="2dp" 
     android:background="@color/main_color" /> 

    <TextView 
     android:id="@+id/dialogText" 
     android:layout_below="@+id/dialogDivider" 
     android:layout_marginLeft="20dp" 
     android:layout_marginTop="10dp" 
     android:layout_marginBottom="10dp" 
     android:textSize="18sp" 
     android:textColor="@color/darkgrey" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</RelativeLayout> 

Vielen Dank im Voraus.

Antwort

0

können Sie verwenden builder.setCustomTitle(content) statt setView(content), aber das wird eine Trennlinie zwischen dem Layout und den Artikel Schaltflächen hinzufügen ...

EDIT:
Als Alternative können Sie die Liste hinzufügen Ihren benutzerdefiniertes Layout, z mit einem Textview wie folgt aus:

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/take_picture" 
    android:layout_below="@id/dialogText" 
    android:layout_marginLeft="20dp" 
    android:layout_marginTop="10dp" 
    android:layout_marginBottom="10dp" 
    android:textSize="26sp" 
    android:onClick="takePicture"/> 

und die Takepicture Methode in MainActivity definieren:

public void takePicture(View view) { 
    Log.i("MainActivity", "take picture"); 
    //cancel the dialog 
    dialog.dismiss(); 
} 

der Dialog in einer Klassenvariablen gespeichert und initialisiert in selectImage():

private void selectImage(){ 
    final CharSequence[] items = { "TAKE_PICTURE", "FROM_GALLERY", "CANCLE"}; 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    LayoutInflater inflater = this.getLayoutInflater(); 
    View title = inflater.inflate(R.layout.alert_dialog, null); 
    builder.setView(title); 
    ((TextView) title.findViewById(R.id.dialogTitle)).setText("picture title"); 
    ((TextView) title.findViewById(R.id.take_picture)).setText(items[0]); 
    //init dialog 
    dialog = builder.create(); 
    dialog.show(); 
} 
+0

Danke. Mein Layout ist oben, aber jetzt wird auch der Standardteiler vor den Elementen angezeigt. –

+0

Ja, ich habe dafür keine Lösung gefunden. Vielleicht kann dies durch Ändern des Themas gelöst werden, weiß nicht – Hugou

Verwandte Themen