純程式碼實現對話方塊

來源:互聯網
上載者:User

標籤:style   class   blog   code   java   ext   

對話方塊自訂:相對布局的java代碼實現
建立AlertDiaglog

Window window = dlg.getWindow()

建立布局,代碼為相對布局

載入布局,載入相關空間,設定相關控制項的位置

代碼如下

        int bgImageViewID = 10;        int iconImageViewID = 11;        int textViewID = 12;        int buttonOkID = 13;        int buttonCancelID = 14;        int srcImageViewId = 15;                final AlertDialog dlg = new AlertDialog.Builder(this).create();        dlg.show();        Window window = dlg.getWindow();        // 主要就是在這裡實現這種效果的.            RelativeLayout rLayout = new RelativeLayout(this);         //設定背景        ImageView bg_iv = new ImageView(this);        bg_iv.setId(bgImageViewID);        bg_iv.setImageResource(R.drawable.bg2);                RelativeLayout.LayoutParams bgRelativeParams_iv = new RelativeLayout.LayoutParams(                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);        bgRelativeParams_iv.addRule(RelativeLayout.CENTER_HORIZONTAL);        rLayout.addView(bg_iv, bgRelativeParams_iv);                ImageView icon_iv = new ImageView(this);        icon_iv.setId(iconImageViewID);        icon_iv.setImageResource(R.drawable.egame_logo_icon);                RelativeLayout.LayoutParams iconRelativeParams_iv = new RelativeLayout.LayoutParams(                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);                iconRelativeParams_iv.addRule(RelativeLayout.ALIGN_LEFT,bgImageViewID);        iconRelativeParams_iv.addRule(RelativeLayout.ALIGN_TOP,bgImageViewID);        rLayout.addView(icon_iv, iconRelativeParams_iv);                        TextView tv = new TextView(this);        tv.setId(textViewID);        tv.setText("XXXXXX");            RelativeLayout.LayoutParams RelativeParams_tv = new RelativeLayout.LayoutParams(                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);            RelativeParams_tv.addRule(RelativeLayout.CENTER_HORIZONTAL,iconImageViewID);        rLayout.addView(tv, RelativeParams_tv);                        ImageView src_iv = new ImageView(this);        src_iv.setId(srcImageViewId);        src_iv.setBackgroundResource(R.drawable.app_icon);            RelativeLayout.LayoutParams srcRelativeParams_iv = new RelativeLayout.LayoutParams(                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);                srcRelativeParams_iv.addRule(RelativeLayout.CENTER_IN_PARENT,bgImageViewID);        //RelativeParams_tv.addRule(RelativeLayout.ALIGN_TOP,bgImageViewID);        rLayout.addView(src_iv, srcRelativeParams_iv);                        Button btn_ok = new Button(this);        btn_ok.setId(buttonOkID);        btn_ok.setBackgroundResource(R.drawable.submit);                RelativeLayout.LayoutParams relativeParams_btn_ok = new RelativeLayout.LayoutParams(                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);                //RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)iv.getLayoutParams();                relativeParams_btn_ok.addRule(RelativeLayout.ALIGN_BOTTOM,bgImageViewID);        relativeParams_btn_ok.addRule(RelativeLayout.ALIGN_LEFT,bgImageViewID);        rLayout.addView(btn_ok, relativeParams_btn_ok);                        Button btn_cancel = new Button(this);        btn_cancel.setId(buttonCancelID);        btn_cancel.setBackgroundResource(R.drawable.cancel);                    RelativeLayout.LayoutParams relativeParams_btn_cancel = new RelativeLayout.LayoutParams(                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);        relativeParams_btn_cancel.addRule(RelativeLayout.ALIGN_BOTTOM,bgImageViewID);        relativeParams_btn_cancel.addRule(RelativeLayout.ALIGN_RIGHT,bgImageViewID);;        rLayout.addView(btn_cancel, relativeParams_btn_cancel);                //window.setContentView(R.layout.diag);        window.setContentView(rLayout);        // 為確認按鈕添加事件,執行退出應用操作        btn_ok.setOnClickListener(new View.OnClickListener() {            public void onClick(View v) {                            }        });        // 關閉alert對話方塊架        btn_cancel.setOnClickListener(new View.OnClickListener() {            public void onClick(View v) {                dlg.cancel();            }        });

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.