使AlertDialog自定义 [英] Make AlertDialog Custom

查看:119
本文介绍了使AlertDialog自定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码显示警告对话框"

I am using this code to display Alert Dialog

  holder.tv1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AlertDialog.Builder nointernetconnection = new AlertDialog.Builder(
                                temp);
                        nointernetconnection
                                .setIcon(R.drawable.ic_launcher)
                                .setTitle(list.get(position).getAS_name())
                                .setMessage(list.get(position).getDesc_art())
                                .setCancelable(true)
                                .setPositiveButton("OK",
                                        new DialogInterface.OnClickListener() {
                                                   @Override
                                            public void onClick(DialogInterface arg,
                                                    int arg1) {



                                            }
                                        });
                        AlertDialog a = nointernetconnection.create();
                        a.show();

如果正文更多,但标题文本尚未完全查看,或者标题空间不可滚动,则消息正文会自动转换为scrollView.

Message body is converted into scrollView automatically in case the text is more but Title text has not been viewed completely nor the title space is scrollable.

因此,我想扩展标题"区域&还希望使其可滚动&为此,我不想使用自定义对话框,我只想通过AlertDialog实现它.

So, I want to expand the Title area & also want to make it scrollable & for this i don't wanna use custom Dialog, i want to only implement it by AlertDialog.

推荐答案

此示例是典型的黑客行为...您也不需要自定义View ...

This example is some what a typical hack... You don't need a custom View also...

private void showDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setIcon(R.drawable.ic_launcher);
    final String title = "This is a Big Title. This is a Big Title. This is a Big Title. This is a Big Title. This is a Big Title. This is a Big Title. ";
    builder.setTitle(title);
    builder.setMessage("This is a Message. This is a Message. This is a Message. This is a Message.");
    builder.setCancelable(false);
    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    AlertDialog alertDialog = builder.create();
    alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            AlertDialog alertDialog = (AlertDialog) dialog;
            ViewGroup viewGroup = (ViewGroup) alertDialog.getWindow()
                    .getDecorView();
            TextView textView = findTextViewWithTitle(viewGroup, title);
            if (textView != null) {
                textView.setEllipsize(null);
                textView.setMaxHeight((int) (80 * alertDialog.getContext().getResources().getDisplayMetrics().density)); 
                textView.setMovementMethod(new ScrollingMovementMethod());
            }
        }
    });
    alertDialog.show();
}

private TextView findTextViewWithTitle(ViewGroup viewGroup, String title) {
    for (int i = 0, N = viewGroup.getChildCount(); i < N; i++) {
        View child = viewGroup.getChildAt(i);
        if (child instanceof TextView) {
            TextView textView = (TextView) child;
            if (textView.getText().equals(title)) {
                return textView;
            }
        } else if (child instanceof ViewGroup) {
            ViewGroup vGroup = (ViewGroup) child;
            return findTextViewWithTitle(vGroup, title);
        }
    }
    return null;
}

这篇关于使AlertDialog自定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆