android dialog 類比新浪、騰訊title彈框效果

來源:互聯網
上載者:User

  首先我們看一下新浪微博的效果(其它就是一個dialog):

                            

                         點擊title前                                                    點擊title後

 實現方式:

     首先我們要自訂一個dialog

    代碼如下:

    

/*** * 自訂dialog *  * @author jia *  */public class MyDialog extends Dialog {private Window window = null;/*** *  * @param context * @param layoutResID *            設定檔 * @param x *            顯示的x座標 * @param y *            顯示的y座標 * @param title *            集合 */public MyDialog(final Context context, int layoutResID, int x, int y,final String[] title) {super(context, R.style.Transparent);window = this.getWindow();window.requestFeature(Window.FEATURE_NO_TITLE);setContentView(layoutResID);int width = this.getWindow().getWindowManager().getDefaultDisplay().getWidth();windowDeploy(width / 2, 300, x, y);show();}/*** * 設定視窗顯示 *  * @param x * @param y * @param dialog_x * @param dialog_y */public void windowDeploy(int dialog_width, int dialog_height, int dialog_x,int dialog_y) {window.setBackgroundDrawableResource(android.R.color.transparent); // 設定對話方塊背景為透明WindowManager.LayoutParams wl = window.getAttributes();wl.width = dialog_width;wl.height = dialog_height;// wl.alpha = 0.8f;wl.gravity = Gravity.LEFT | Gravity.TOP; // 不設定的話預設是置中wl.x = dialog_x - dialog_width / 2; // 要顯示的位置x座標wl.y = dialog_y;window.setAttributes(wl);window.setWindowAnimations(R.style.dialogWindowAnim); // 設定視窗彈齣動畫setCanceledOnTouchOutside(true);}}

 我們只需要在activity中調用即可:

 程式碼片段:

 

textView.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {int x_begin = textView.getLeft();int x_end = textView.getRight();int y_begin = textView.getTop();int y_end = textView.getBottom();// 這個是要顯示位置的縱座標// 擷取最中間的x座標int x = (x_begin + x_end) / 2;// 這個值也就是螢幕最中間的值,也可以下面這樣// int x=getWindowManager().getDefaultDisplay().getWidth()/2;// int[] location = new int[2];// textView.getLocationInWindow(location); // 擷取在當前視窗內的絕對座標// textView.getLocationOnScreen(location);// 擷取在整個螢幕內的絕對座標myDialog = new MyDialog(DialogDemoActivity.this,R.layout.dialog, x, y_end, title);View view = LayoutInflater.from(DialogDemoActivity.this).inflate(R.layout.dialog, null);listView = (ListView) myDialog.getWindow().findViewById(R.id.lv_dialog);listView.setAdapter(new ArrayAdapter<String>(DialogDemoActivity.this, R.layout.text, R.id.tv_text,title));listView.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {textView.setText(title[arg2]);myDialog.cancel();myDialog = null;}});}});

實現效果如下:

          
 

           點擊前                                                點擊後                                             選擇                                                   選擇後


實現起來也不難,有點要說明一下,這裡我們用到了.9.png圖片,這個圖片會自動根據需要伸展,(重要的是不失真,這點很棒吧,詳細介紹請點擊串連).

 源碼下載



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.