public TimeViewHolder(Context activity, View view) {this.activity = activity;this.root = view;}
如何使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可以看作是一個佈局,所有的控件都以它爲載體
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、收工
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.