ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 如何调整AlertDialog.Builder的大小?

2019-10-03 13:28:02  阅读:593  来源: 互联网

标签:android-alertdialog android resize


我想调整我的AlertDialog.Builder的大小,但我没有在这个网站上找到任何解决方案,我尝试了我在this one这样的问题上看到的所有东西,但看起来比我想要的还要老,我正在看的代码没有现在就工作……

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以调整其大小,但只显示一个没有数据的黑色方块.

问题:如何使用大文本制作AlertDialog.Builder? (需要滚动肯定,因为我不想让对话框像我的屏幕一样大)

更新:

我想要的是这样的东西,我必须通过Bundle更改文本,我从我的AppCompatActivity调用AlertDialog使用FrameLayout(我不知道应该用什么样的Dialog来覆盖我需要的东西):

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");
...............

我的对话框显示使用DEFAULT LAYOUT(它没有scrollview也没有我感兴趣的大小(只显示为默认的android警告对话框……)

我该如何解决这个问题?我认为这很愚蠢,但令人沮丧……无论如何,谢谢!

解决方法:

您应该使用自定义对话框布局,请检查documentation.

实际上,你可以通过getDialog()调整对话框.getWindow().setLayout(width,height);只需将其命名为onResume(而不是onCreateView).

您的视图必须嵌套在ScrollView中,请检查post.

在Dialog中读取大量文本可能很烦人,最好使用你拥有的所有像素,考虑简单的Activity / Fragment.

标签:android-alertdialog,android,resize
来源: https://codeday.me/bug/20191003/1849477.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有