2016-04-21 91 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 
} 

更多信息:

相关问题