簡體   English   中英

警報對話框中未顯示警報消息?

[英]Alert message is not displaying in alert dialog box?

我想顯示警報對話框以顯示設備屏幕尺寸。 我通過此鏈接獲得了解決方案如何使警報對話框填充屏幕大小的90%? 我使用那里提供的解決方案,效果很好,但是我的警報消息以很小的尺寸顯示。 我們甚至無法橫向傳達信息。 我使用了以下代碼。

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.app_description).setPositiveButton(
                    "Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.dismiss();
                        }
                    });
            builder.Title(title);

            Dialog d = builder.setView(new View(this)).create();
            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
            lp.copyFrom(d.getWindow().getAttributes());
            lp.width = WindowManager.LayoutParams.FILL_PARENT;
            lp.height = WindowManager.LayoutParams.FILL_PARENT;
            d.show();
            d.getWindow().setAttributes(lp);

如何設置消息,使其正確顯示在對話框窗口中?

在此先感謝Pushpa

默認情況下,您無法設置文本大小。 您可以做一件簡單的事情。 編寫XML並對該XML進行充氣,然后傳遞到構建器視圖。

   builder.setView(view)  

什么都沒有,但您正在將視圖設置為對話框。 並且該視圖將處理所有觸摸。 並且您可以在xml中指定視圖的高度和寬度,包括文本大小。

檢查此代碼,然后告訴我您是否真的想要實現?

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

    // Creates textview
    TextView text = new TextView(this);  
    text.setText("Hello This text");  
    text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    text.setTextSize(20);        
    text.setGravity(Gravity.CENTER);

    //Creates a linearlayout layout and sets it with initial params
    LinearLayout ll = new LinearLayout(this);
    ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    ll.setGravity(Gravity.CENTER);
    ll.addView(text);  //adds textview to llayout

    builder.setMessage("Title").setPositiveButton(
            "Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
                }
            }); 

    Dialog d = builder.setView(ll).create();   

    //Fills up the entire Screen
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(d.getWindow().getAttributes());
    lp.width = WindowManager.LayoutParams.FILL_PARENT;
    lp.height = WindowManager.LayoutParams.FILL_PARENT;
    d.show();
    d.getWindow().setAttributes(lp);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM