自定义dialog之屏幕右边选择时间

先是弹窗的样式

 

0 <!-- 弹窗样式 -->
1
<style name="dialog" parent="@android:style/Theme.Dialog"> 2   <item name="android:windowFrame">@null</item> 3   <item name="android:windowIsFloating">true</item> 4   <item name="android:windowIsTranslucent">false</item> 5   <item name="android:background">@android:color/transparent</item> 6   <item name="android:windowBackground">@android:color/transparent</item> 7   <item name="android:windowNoTitle">true</item> 8   <item name="android:backgroundDimEnabled">false</item> 9 </style>

 

然后是java代码

  1 /**

  2 * 弹出时间选择控件

  3 * 

  4 * @Description:从右边弹出时间选择控件

  5 * @author sleep

  6 * @date create date 2013年12月24日

  7 * @version V1.0

  8 */

  9 public class PopDateWindow extends Dialog

 10 {

 11 private Window window = null;

 12 private Context context = null;

 13 private ListView list;

 14 private ArrayList<String> dates;

 15 

 16 public PopDateWindow(Context context, ArrayList<String> dates)

 17 {

 18 super(context, R.style.dialog);

 19 this.context = context;

 20 this.dates = dates;

 21 showDialog();

 22 }

 23 

 24 public void showDialog()

 25 {

 26 showDialog(0, 0);

 27 }

 28 

 29 public void showDialog(int x, int y)

 30 {

 31 RelativeLayout rl = new RelativeLayout(context);

 32 list = new ListView(context);

 33 

 34 list.setVerticalScrollBarEnabled(false);

 35 RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(120,

 36 LayoutParams.MATCH_PARENT);

 37 lp.addRule(RelativeLayout.ALIGN_TOP);

 38 lp.addRule(RelativeLayout.ALIGN_RIGHT);

 39 rl.addView(list, lp);

 40 

 41 setContentView(rl);

 42 list.setAdapter(new MyBaseAdapter(context, dates));

 43 // 界面设置

 44 windowDeploy(x, y);

 45 }

 46 

 47 /**

 48 * 设置监听

 49 * 

 50 * @param itemListener

 51 */

 52 public void setOnListItemClickListener(OnItemClickListener itemListener)

 53 {

 54 list.setOnItemClickListener(itemListener);

 55 }

 56 

 57 /**

 58 * 设置窗口显示

 59 * 

 60 * @param x

 61 * @param y

 62 */

 63 private void windowDeploy(int x, int y)

 64 {

 65 window = getWindow();

 66 // window.setWindowAnimations(R.style.dialogWindowAnim);

 67 window.setBackgroundDrawableResource(R.color.grey);

 68 WindowManager.LayoutParams wl = window.getAttributes();

 69 wl.x = x;

 70 wl.y = y;

 71 wl.alpha = 1f;

 72 wl.gravity = Gravity.RIGHT;

 73 window.setAttributes(wl);

 74 // super.setCanceledOnTouchOutside(true);//单击空白处,隐藏dialog

 75 }

 76 

 77 class MyBaseAdapter extends BaseAdapter

 78 {

 79 private List<String> dates;

 80 private Context context;

 81 

 82 public MyBaseAdapter(Context context, List<String> dates)

 83 {

 84 super();

 85 this.context = context;

 86 this.dates = dates;

 87 }

 88 

 89 @Override

 90 public int getCount()

 91 {

 92 // TODO Auto-generated method stub

 93 return dates.size();

 94 }

 95 

 96 @Override

 97 public Object getItem(int arg0)

 98 {

 99 // TODO Auto-generated method stub

100 return dates.get(arg0);

101 }

102 

103 @Override

104 public long getItemId(int arg0)

105 {

106 // TODO Auto-generated method stub

107 return arg0;

108 }

109 

110 @Override

111 public View getView(final int position, View convertView,

112 ViewGroup parent)

113 {

114 LinearLayout ll = new LinearLayout(context);

115 TextView text = new TextView(context);

116 text.setLayoutParams(new LayoutParams(120, 50));

117 text.setText(dates.get(position));

118 text.setGravity(Gravity.CENTER);

119 text.setTextSize(20);

120 text.setLines(1);

121 ll.addView(text);

122 return ll;

123 }

124 

125 }

126 

127 }

最后是效果

自定义dialog之屏幕右边选择时间

你可能感兴趣的:(dialog)