2017-09-15 75 views
0

我有我自己的AlertDialog佈局,如果我使用setPositiveButton,一切正常。但是,當我使用setItems時,我的佈局顯示在項目按鈕下方。AlertDialog按鈕下面的佈局

如何在頂部顯示我的自定義佈局?

這裏是我的代碼:

 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(); 
     } 
    } 

而且我的佈局:

<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> 

在此先感謝。

回答

0

可以使用builder.setCustomTitle(content)代替setView(content),但是這會增加你的佈局和項目按鈕之間的隔離線...

編輯:
作爲替代方案,您可以將列表添加到您的自定義佈局,例如像這樣一個TextView的:

<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"/> 

,並確定在MainActivity的takePicture方法:

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

對話框被保存在一個類變量,並初始化在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

謝謝。我的佈局在上面,但現在在項目之前也顯示了標準分隔線。 –

+0

是的,我沒有找到解決方案。也許這可以通過改變主題來解決,不知道 – Hugou