怎样使Dialog像Activity一样随心所欲的使用?

怎样使Dialog像Activity一样随心所欲的使用?
android中的Dialog像是寄生在Activity中。在弹出Dialog时。因受到系统风格定义,导致Dialog怎么也不能如意,那么今天就来探讨下Dialog的使用。

要全然自己定义一个Dialog。那么就不要extends AlertDialog。我们在使用Activity时,先准备一个布局文件xxx.xml,全部控件大小都能够在布局文件里写好。

事实上Dialog也能够这么做。


实现步骤:
     1、准备好布局文件: 
     2、组织布局中的控件动作。写成一个ViewHolder,我这里写的是 TimeViewHolder,构造函数中要两个參数:Context和View,这个                                                       ViewHolder与Activity的ViewHolder不一样,由于Activity是带布局的。所以能够不要View这个參数。

这个View能够看作是一个布局,全部的控件都以它为载体

public  TimeViewHolder(Context activity, View view) {
      this . activity  = activity;         
     this . root  = view;
    }
     2、准备好一套风格:这个风格文件定义在res/values/styles.xml中,当然能够写到其它风格文件里
         <  style   name =  "picture_mode"   parent  = "@android:Theme.Dialog"  >
              <  item   name  = "android:windowFrame"  > @null  </  item >
              <  item   name  = "android:windowIsFloating"  > true  </  item >
              <  item   name  = "android:windowIsTranslucent"  > false  </  item >
              <  item   name  = "android:windowNoTitle"  > true  </  item >  <!--除去title-->
              <  item   name  = "android:windowContentOverlay"  > @null  </  item >
              <  item   name  = "android:backgroundDimEnabled"  > false  </  item >
              <  item   name  = "android:windowBackground"  > @null  </  item >  <!--除去背景色-->
          </ style  >
     3、在Activity中创建Dialog:
           Dialog timeDialog =  new  Dialog(  mActivity  ,
                    R.style.  picture_mode  ) {//创建Dialog时。指定风格。带一个Content參数:mActivity
                  @Override
                  protected   void  onCreate(Bundle savedInstanceState) {
                      //  TODO  Auto-generated method stub
                      super  .onCreate(savedInstanceState);
                    setContentView(R.layout.  time  );//在onCreate方法中,指定布局文件,因上面指定了风格。所以这个
                                                    //Dialog的大小位置全都会因这个layout决定,所以你想如何就如何
                    View mView=getWindow().getDecorView();//关键在这里,这个View,是你依据xml中的id来找相应控件的。

                      if  ( timeViewHolder  ==  null ) {
                          timeViewHolder  =  new  TimeViewHolder( mActivity   ,
                                mView);
                    }

                }

                  @Override
                  protected   void  onStart() {//在这种方法中,你能够实现一些交互动作
                      //  TODO  Auto-generated method stub
                      super  .onStart();

                    Log. i(  "ytmfdw" "timeDialog onStart" );
                      timeViewHolder  .findViews();
                }

                  @Override
                  public   boolean  dispatchKeyEvent(KeyEvent event) {//按键动作。
                      //  TODO  Auto-generated method stub
                    
                      return   super  .dispatchKeyEvent(event);
                }
            };
            timeDialog.setOnDismissListener(  new  OnDismissListener() {//当Dialog消失时,做一些处理
                                                                      //由于这个Dialog出现时,我把Activity隐藏了,
                                                                      //所以在Dialog消失时,我要把Activity显示出来

                  @Override
                  public   void  onDismiss(DialogInterface dialog) {
                      //  TODO  Auto-generated method stub
                      linearlayout_tvset_menu  .setVisibility(View.  VISIBLE );//Activity显示出来
                      linearlayout_set_time  .requestFocus();
                      timeViewHolder  =  null  ;

                }
            });

            timeDialog.show();//Dialog创建完毕后。要显示的
     4、在ViewHolder中使用Dialog的布局中的控件:
   private  View  root  ;
      private  TextView  tv_time_date  ;
     public  TimeViewHolder(Context activity, View view) {
          this  . activity  = activity;
          this  . root  = view;
          tvTimerManager  = TvTimerManager.getInstance();
    }

      public   void  findViews() {
          tv_time_date  = (TextView)  root  .findViewById(R.id.  tv_time_date );
        loadDataToUI();
    }
          
     5、收工
          
     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值