1. 程式人生 > >怎樣使Dialog像Activity一樣隨心所欲的使用?

怎樣使Dialog像Activity一樣隨心所欲的使用?

dism super values con lean 背景 處理 步驟 auto

怎樣使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、收工

怎樣使Dialog像Activity一樣隨心所欲的使用?