首页 文章

Android:如何创建没有 Headers 的Dialog?

提问于
浏览
254

我正在尝试在Android中生成自定义对话框 . 我像这样创建我的Dialog:

dialog = new Dialog(this);
dialog.setContentView(R.layout.my_dialog);

除了Dialog的 Headers 之外,Everythings工作正常 . 即使我没有设置对话框的 Headers ,对话框弹出窗口在对话框的位置也有一个空格 .

有没有办法隐藏Dialog的这一部分?

我尝试使用AlertDialog,但似乎布局设置不正确:

LayoutInflater inflater = 
    (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.map_dialog, null);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(view);

// dialog = new Dialog(this);
// dialog.setContentView(R.layout.map_dialog);

dialog = builder.create();

((TextView) dialog.findViewById(R.id.nr)).setText(number);

如果我使用这段代码,我会在最后一行得到一个空指针异常 . 该对话框不为null,因此我尝试检索的TextView不存在 .
如果我取消注释我使用Dialog构造函数的部分,一切正常,但对于我的对话框布局上方的 Headers .

24 回答

  • 2

    将整个对话框中的“gravity”属性设置为“center” . 然后,您需要将该设置覆盖到对话框中不希望居中的所有子组件 .

  • 60
    dialog=new Dialog(YourActivity.this, 1);  // to make dialog box full screen with out title.
    dialog.setContentView(layoutReference);
    dialog.setContentView(R.layout.layoutexample);
    
  • 582

    在XML中使用主题

    android:theme="@android:style/Theme.NoTitleBar"
    
  • 38

    在Custom_Dialog.java类中添加 requestWindowFeature(Window.FEATURE_NO_TITLE)

    public class Custom_Dialog extends Dialog {
    
        protected Custom_Dialog(Context context, int theme) {
            super(context, theme);
            // TODO Auto-generated constructor stub
            requestWindowFeature(Window.FEATURE_NO_TITLE); //This line 
        }
    }
    
  • 29

    olivierg's answer为我工作,如果创建自定义Dialog类是您想要的路线,那么这是最好的解决方案 . 但是,我无法使用AlertDialog类 . 我希望能够使用默认的系统AlertDialog样式 . 创建自定义对话框类将不具有此样式 .

    所以我找到了一个无需创建自定义类就可以工作的解决方案(hack),您可以使用现有的构建器 .

    AlertDialog将View放在内容视图上方作为 Headers 的占位符 . 如果找到视图并将高度设置为0,则空间消失 .

    到目前为止,我已经在2.3和3.0上对此进行了测试,但它可能无法在每个版本上运行 .

    以下是两种辅助方法:

    /**
     * Show a Dialog with the extra title/top padding collapsed.
     * 
     * @param customView The custom view that you added to the dialog
     * @param dialog The dialog to display without top spacing
         * @param show Whether or not to call dialog.show() at the end.
     */
    public static void showDialogWithNoTopSpace(final View customView, final Dialog dialog, boolean show) {
        // Now we setup a listener to detect as soon as the dialog has shown.
        customView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    
            @Override
            public void onGlobalLayout() {
                // Check if your view has been laid out yet
                if (customView.getHeight() > 0) {
                    // If it has been, we will search the view hierarchy for the view that is responsible for the extra space. 
                    LinearLayout dialogLayout = findDialogLinearLayout(customView);
                    if (dialogLayout == null) {
                        // Could find it. Unexpected.
    
                    } else {
                        // Found it, now remove the height of the title area
                        View child = dialogLayout.getChildAt(0);
                        if (child != customView) {
                            // remove height
                            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams();
                            lp.height = 0;
                            child.setLayoutParams(lp);
    
                        } else {
                            // Could find it. Unexpected.
                        }
                    }
    
                    // Done with the listener
                    customView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
             }
    
        });
    
        // Show the dialog
        if (show)
                 dialog.show();
    }
    
    /**
     * Searches parents for a LinearLayout
     * 
     * @param view to search the search from
     * @return the first parent view that is a LinearLayout or null if none was found
     */
    public static LinearLayout findDialogLinearLayout(View view) {
        ViewParent parent = (ViewParent) view.getParent();
        if (parent != null) {
            if (parent instanceof LinearLayout) {
                // Found it
                return (LinearLayout) parent;
    
            } else if (parent instanceof View) {
                // Keep looking
                return findDialogLinearLayout((View) parent);
    
            }
        }
    
        // Couldn't find it
        return null;
    }
    

    以下是如何使用它的示例:

    Dialog dialog = new AlertDialog.Builder(this)
            .setView(yourCustomView)
            .create();
    
        showDialogWithNoTopSpace(yourCustomView, dialog, true);
    

    如果您使用DialogFragment,请覆盖DialogFragment的 onCreateDialog 方法 . 然后像上面的第一个例子一样创建并返回对话框 . 唯一的变化是你应该传递false作为第三个参数(show),这样它就不会在对话框上调用show() . DialogFragment稍后会处理 .

    例:

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new AlertDialog.Builder(getContext())
            .setView(yourCustomView)
            .create();
    
        showDialogWithNoTopSpace(yourCustomView, dialog, false);
        return dialog;
    }
    

    当我进一步测试时,我将确保更新任何其他内容需要调整 .

  • 198

    我不知道这个问题是否仍然存在,但就我而言,当我从Dialog切换到DialogFragment时,

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    

    不是一个选择,但我可以使用

    setStyle(STYLE_NO_TITLE, 0);
    

    相反,结果相同 .

  • 7

    您可以使用以下方法隐藏对话框的 Headers :

    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);


    此答案的先前版本过于复杂:

    您需要使用 AlertDialog . 有's a good explanation on the Android Developer'的网站关于custom dialogs .

    在非常简短的总结中,您可以使用以下从官方网站复制的代码执行此操作 . 这需要一个自定义的layot文件,膨胀它,给它一些基本的文本和图标,然后创建它 . 然后用 alertDialog.show() 显示它 .

    AlertDialog.Builder builder;
    AlertDialog alertDialog;
    
    Context mContext = getApplicationContext();
    LayoutInflater inflater = (LayoutInflater)
            mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.custom_dialog,
            (ViewGroup) findViewById(R.id.layout_root));
    
    TextView text = (TextView) layout.findViewById(R.id.text);
    text.setText("Hello, this is a custom dialog!");
    ImageView image = (ImageView) layout.findViewById(R.id.image);
    image.setImageResource(R.drawable.android);
    
    builder = new AlertDialog.Builder(mContext);
    builder.setView(layout);
    alertDialog = builder.create();
    

    回应评论:

    我假设具有id nr 的TextView位于您正在使用 View view = inflater.... 充气的视图中 . 如果是这样,那么你只需改变一位:而不是 dialog.findView... 使其成为 view.findView... . 然后,一旦你完成了,记得使用dialog.show(),甚至builder.show(),而不必费心去做builder.create() .

  • 2

    从头开始创建对话框时,FEATURE_NO_TITLE有效,如:

    Dialog dialog = new Dialog(context);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    

    但是在创建AlertDialog(或使用Builder)时它不起作用,因为它已经禁用了 Headers 并在内部使用自定义 Headers .

    我查看了SDK源代码,我认为它无法解决 . 因此,要删除顶部间距,唯一的解决方案是直接使用Dialog类从头开始创建自定义对话框IMO .

    此外,可以使用样式执行此操作,例如在styles.xml中:

    <style name="FullHeightDialog" parent="android:style/Theme.Dialog">
       <item name="android:windowNoTitle">true</item>
    </style>
    

    然后:

    Dialog dialog = new Dialog(context, R.style.FullHeightDialog);
    
  • 2

    在您的代码中添加此行

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    

    或者在XML中使用主题

    android:theme="@android:style/Theme.NoTitleBar"
    

    XML将是一个更好的实现,因为 Headers 栏被创建然后被删除,这是浪费资源的代码版本

    好的尝试,但它不起作用 . 我得到:android.view.WindowManager $ BadTokenException:无法添加窗口 - 如果我想要对话框,则令牌null不适用于应用程序 .

    将警报对话框类型更改为系统对话框(例如,TYPE_SYSTEM_OVERLAY)并查看是否可以解决您的问题

  • 3

    使用这样:

    Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    

    这将从对话框窗口中删除任何 Headers 栏 .

  • 10

    setcontentview 之前使用以下代码: -

    Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
        dialog.setContentView(R.layout.custom_dialog);
    

    Note :您必须拥有相同的顺序和行的上述代码 . requestWindowFeature 必须是 before setContentView行 .

  • 1

    你可以删除 Headers

    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    

    对话框是我的对话框的名称 .

  • 2

    在您的代码中,如果您使用 requestWindowFeature(Window.FEATURE_NO_TITLE); ,请确保它在 dialog.setContentView(); 之前,否则会导致应用程序崩溃 .

  • 3

    我找到了三种方法来做到这一点>

    1)使用requestWindowFeature

    Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(dialog.getWindow().FEATURE_NO_TITLE);
    

    2)使用style(style.xml)

    <style name="FullHeightDialog" parent="android:style/Theme.Dialog">
       <item name="android:windowNoTitle">true</item>
    </style>
    
    Dialog dialog = new Dialog(context, R.style.FullHeightDialog);
    

    3)在AndroidManifest.xml中使用XML主题

    android:theme="@android:style/Theme.NoTitleBar"
    
  • 67

    使用构建器将 Headers 设置为空字符串 .

    Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("");
    ...
        builder.show();
    
  • 1

    如果我们只使用没有 setTitle() 的对话框,那么是否可以删除 Headers 空间?

    mUSSDDialog = new AlertDialog.Builder(context).setView(dialogView)
    .setPositiveButton(R.string.send_button,DialogListener)
    .setNegativeButton(R.string.cancel,DialogListener)
    .setCancelable(false).create();
    
  • 4

    认为你现在可以使用它:

    AlertDialog dialog = new AlertDialog.Builder(this)
      .setView(view)
      .setTitle("")
      .create()
    
  • 57

    您可以在不使用 AlertDialog 的情况下通过定义从 Dialog 类扩展的新类来执行此操作,如下所示:

    public class myDialog extends Dialog {
        public myDialog(Context context) {
            super(context);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
        }
    }
    
  • 10
    ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "", 
                                 "Loading. Please wait...", true);
    

    创建一个 Headers 少的对话框

  • 3
    public static AlertDialog showAlertDialogWithoutTitle(Context context,String msg) 
         {
          AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
          alertDialogBuilder.setMessage(msg).setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int id) {
    
             }
            });
    
           return alertDialogBuilder.create(); 
         }
    
  • 3

    使用AlertDialog时,不使用 setTitle() 会使 Headers 消失

  • 6

    经过一堆黑客攻击,我得到了这个工作:

    Window window = dialog.getWindow();
                View view = window.getDecorView();
                final int topPanelId = getResources().getIdentifier( "topPanel", "id", "android" );
                LinearLayout topPanel = (LinearLayout) view.findViewById(topPanelId);
                topPanel.setVisibility(View.GONE);
    
  • 1

    这是你可以用 AlertBuilder 做的事情,使 Headers 消失:

    TextView title = new TextView(this);
    title.setVisibility(View.GONE);
    builder.setCustomTitle(title);
    
  • 3

    用这个

    Dialog dialog = new Dialog(getActivity());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.setCancelable(true);
        dialog.setContentView(R.layout.image_show_dialog_layout);
    

相关问题