如何调整AlertDialog.Builder的大小? [英] How to resize an AlertDialog.Builder?
问题描述
我想调整我的AlertDialog.Builder
大小,但在此网站上找不到任何分辨率,我尝试了在
I'd like to resize my AlertDialog.Builder
but I didn't find any resolution on this website, I tried everything I saw on questions like this one but seems older than I'm looking for, The code I'm watching doesn't work right now...
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.Builder
更改为AlertDialog
来调整其大小,但只显示了一个没有数据的黑色正方形.
I tried to become AlertDialog.Builder
to AlertDialog
to resize it, but just displays a black square without data.
问题:如何制作带有大文本的AlertDialog.Builder? (需要滚动确定,因为我不想让对话框和我的屏幕一样大)
QUESTION: What should I do to make an AlertDialog.Builder with a large text? (need to scroll that sure, because I don't want to make the dialog as big as my screen)
已更新:
我想要的是这样的东西,我必须通过Bundle
更改文本,我从使用FrameLayout
的AppCompatActivity调用AlertDialog(我不知道它应该是哪种类型的Dialog来覆盖什么内容)我需要):
What I want is something like this, I HAVE TO CHANGE THE TEXT by Bundle
, I call the AlertDialog from my AppCompatActivity which uses FrameLayout
(I don't know what kind of Dialog it should be to cover what I need):
更新2 :
最后,我一直在尝试使用下一个代码来完成所需的工作,但仍然无法正常工作...
Finally I've been trying to do what I'm looking for with the next code, but it still doesn't work...
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");
...............
我的对话框使用DEFAULT LAYOUT显示(它没有滚动视图,也没有我感兴趣的大小(仅显示为默认的android警报对话框...)
My dialog appears using DEFAULT LAYOUT (It doesn't have scrollview nor my interested size (just shows as default android alert dialog...)
我该如何解决这个问题?我认为这很愚蠢,但令人沮丧...还是谢谢!!
How can I resolve this question? I think that's silly, but it's frustating... Thanks anyway!
推荐答案
You should use custom dialog layout, check documentation.
实际上,您可以通过getDialog().getWindow().setLayout(width, height);
调整对话框的大小,只需将其命名为onResume(而不是onCreateView)即可.
Actually, you can resize dialog by getDialog().getWindow().setLayout(width, height);
just call it onResume (not onCreateView).
您的视图必须嵌套在ScrollView中,请检查发布.
Your view must be nested inside of a ScrollView, check post.
在对话框中读取大量文本可能很烦人,最好使用所有像素,考虑简单的Activity/Fragment.
Reading huge amount of text in a Dialog can be annoying, it's better to use all pixels you have, consider simple Activity/Fragment.
这篇关于如何调整AlertDialog.Builder的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!