0
点赞
收藏
分享

微信扫一扫

android dialog 模拟新浪、腾讯title弹框效果


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

              

android dialog 模拟新浪、腾讯title弹框效果_移动开发

              

android dialog 模拟新浪、腾讯title弹框效果_java_02

                         点击title前                                                    点击title后

 实现方式:

     首先我们要自定义一个dialog

    代码如下:

    


[java]  view plain copy

 


 

1. /***
2.  * 自定义dialog
3.  * 
4.  * @author jia
5.  * 
6.  */  
7. public class MyDialog extends Dialog {  
8. private Window window = null;  
9.   
10. /***
11.      * 
12.      * @param context
13.      * @param layoutResID
14.      *            配置文件
15.      * @param x
16.      *            显示的x坐标
17.      * @param y
18.      *            显示的y坐标
19.      * @param title
20.      *            集合
21.      */  
22. public MyDialog(final Context context, int layoutResID, int x, int y,  
23. final String[] title) {  
24. super(context, R.style.Transparent);  
25.   
26. this.getWindow();  
27.         window.requestFeature(Window.FEATURE_NO_TITLE);  
28.         setContentView(layoutResID);  
29. int width = this.getWindow().getWindowManager().getDefaultDisplay()  
30.                 .getWidth();  
31. 2, 300, x, y);  
32.         show();  
33.   
34.     }  
35.   
36. /***
37.      * 设置窗口显示
38.      * 
39.      * @param x
40.      * @param y
41.      * @param dialog_x
42.      * @param dialog_y
43.      */  
44. public void windowDeploy(int dialog_width, int dialog_height, int dialog_x,  
45. int dialog_y) {  
46.   
47. // 设置对话框背景为透明  
48.         WindowManager.LayoutParams wl = window.getAttributes();  
49.         wl.width = dialog_width;  
50.         wl.height = dialog_height;  
51. // wl.alpha = 0.8f;  
52. // 不设置的话默认是居中  
53. 2; // 要显示的位置x坐标  
54.         wl.y = dialog_y;  
55.         window.setAttributes(wl);  
56. // 设置窗口弹出动画  
57. true);  
58.     }  
59.   
60. }


 

 

 我们只需要在activity中调用即可:

 代码片段:

 


[java]  view plain copy

 

1. textView.setOnClickListener(new OnClickListener() {  
2. @Override  
3. public void onClick(View v) {  
4.   
5. int x_begin = textView.getLeft();  
6. int x_end = textView.getRight();  
7. int y_begin = textView.getTop();  
8. int y_end = textView.getBottom();// 这个是要显示位置的纵坐标  
9. // 获取最中间的x坐标  
10. int x = (x_begin + x_end) / 2;// 这个值也就是屏幕最中间的值,也可以下面这样  
11. // int x=getWindowManager().getDefaultDisplay().getWidth()/2;  
12.   
13. // int[] location = new int[2];  
14. // textView.getLocationInWindow(location); // 获取在当前窗口内的绝对坐标  
15. // textView.getLocationOnScreen(location);// 获取在整个屏幕内的绝对坐标  
16.   
17. new MyDialog(DialogDemoActivity.this,  
18.                         R.layout.dialog, x, y_end, title);  
19.   
20. this)  
21. null);  
22.                 listView = (ListView) myDialog.getWindow().findViewById(  
23.                         R.id.lv_dialog);  
24. new ArrayAdapter<String>(  
25. this, R.layout.text, R.id.tv_text,  
26.                         title));  
27.   
28. new OnItemClickListener() {  
29.   
30. @Override  
31. public void onItemClick(AdapterView<?> arg0, View arg1,  
32. int arg2, long arg3) {  
33.                         textView.setText(title[arg2]);  
34.                         myDialog.cancel();  
35. null;  
36.                     }  
37.   
38.                 });  
39.             }  
40.         });

实现效果如下:

 

 

android dialog 模拟新浪、腾讯title弹框效果_移动开发_03

     

android dialog 模拟新浪、腾讯title弹框效果_移动开发_04

   

android dialog 模拟新浪、腾讯title弹框效果_源码下载_05

   

android dialog 模拟新浪、腾讯title弹框效果_java_06

           点击前                                                点击后                                             选择                                                   选择后

 

实现起来也不难,有点要说明一下,这里我们用到了.9.png图片,这个图片会自动根据需要伸展


举报

相关推荐

0 条评论