2016-04-21 96 views
0

我想調整我AlertDialog.Builder,但我沒有發現本網站上的任何決定,我都試過了我的問題,看到了像this one但是好像年紀比我在尋找,我在看代碼不現在工作...如何調整AlertDialog.Builder的大小?

ALERTDIALOGBUILDER.JAVA

public class AlertDialogInfo extends AlertDialog.Builder { 

public AlertDialogInfo(Context context, String title, String msg) { 
    super(context); 
    AlertDialog.Builder builder = new AlertDialog.Builder(context,R.style.AppCompatAlertDialogStyle); 
    builder.setTitle(title); 
    builder.setMessage(msg); 
    builder.setNegativeButton("OK",new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.dismiss(); 
     } 
    }); 
    } 
} 

我試圖成爲AlertDialog.BuilderAlertDialog調整其大小,只是沒有顯示數據的黑色正方形。

問題:我應該怎麼做一個AlertDialog.Builder與大文本? (需要滾動是肯定的,因爲我不想讓對話框一樣大,我的屏幕)

修訂

我想是這樣的,我必須要更改文本Bundle,我稱之爲AlertDialog從我AppCompatActivity它採用FrameLayout(我不知道這應該是涵蓋什麼,我需要什麼樣的對話):

WhatIWant

更新2

最後,我一直在試圖做什麼,我正在尋找下一個代碼,但它仍然無法正常工作......

ALERT_DIALOG.XML

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="250dp"> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:scrollbarAlwaysDrawVerticalTrack="true" 
    android:scrollbars="vertical"> 

    <TextView 
     android:id="@+id/msg" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="14sp" /> 

</LinearLayout> 

MYALERTDIALOG.JAVA

public class MyDialogFragment extends DialogFragment { 

public static MyDialogFragment newInstance(String text) { 
    MyDialogFragment frag = new MyDialogFragment(); 
    Bundle args = new Bundle(); 
    args.putString("TEXT", text); 
    System.out.print("Text dentro de newInstance: " + text); 
    frag.setArguments(args); 
    return frag; 
} 

@NonNull 
@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    String sinopsis = getArguments().getString("TEXT"); 
    System.out.print("Text dentro de onCreateDialog: " + text); 
    return new AlertDialog.Builder(getActivity()) 
      .setIcon(android.R.drawable.ic_dialog_info) 
      .setTitle("TITLE") 
      .setMessage(text) 
      .setNegativeButton("OK", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int whichButton) { 
          dismiss(); 
         } 
        } 
      ) .create(); 
} 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.alert_dialog, container); 
} 
} 

ActivityWhereDialogIsBeenCalled.java

........ 
DialogFragment frag = MyDialogFragment.newInstance("MY MESSAGE"); 
     frag.show(getSupportFragmentManager(),"TAG"); 
............... 

我的對話框出現時使用默認的佈局(它不具有滾動視圖,也沒有我的興趣大小(只顯示爲默認的Android警告對話框...)

我怎樣才能解決這個問題?我認爲這很愚蠢,但這令人沮喪......無論如何,謝謝!

+0

http://www.mkyong.com/ android/android-custom-dialog-example/ –

回答

0

你應該使用自定義對話框佈局,檢查documentation

其實,你可以通過getDialog().getWindow().setLayout(width, height);調整對話框只需要調用它的onResume(不onCreateView)。

你的觀點必須嵌套滾動型的內部,檢查post

在對話框讀取文本的巨量,可惱人的,它是更好地使用你的所有像素,可以考慮簡單的活動/片段。

+0

謝謝你Maxim,你能告訴我如何使不全屏的DialogFragment?這似乎是相同的錯誤... –

+0

我試過... setLayout(),但它不工作:( –

+0

我已經看到這篇文章,似乎是我在找,謝謝。但什麼是最好的從對話框調用的活動中使用Bundle更改對話框消息的方法 –

-1

如果您使用的是DialogFragment內的AlertDialog,與AppCompat支持庫23.2.1或以上,則必須把調整大小代碼DialogFragment.onStart(),否則將無法正常工作。事情是這樣的:

@Override 
public void onStart() { 
    super.onStart(); 
    // Method 1 
    alertDialog.getWindow().getAttributes().width = 400; // pixels 
    alertDialog.getWindow().getAttributes().height = 500; // pixels 
    // Re-apply the modified attributes 
    alertDialog.getWindow().setAttributes(
      alertDialog.getWindow().getAttributes()); 

    // Method 2 (alternative) 
    alertDialog.getWindow().setLayout(400, 500); // size in pixels 
} 

更多信息:

相關問題