protected void onResume() {
super.onResume();
initTopLayout(this);
Log.i("BaseActivity", "onResume");
setTop(leftBtn, titleTv, rightTv, rightIv);
}
<span style="font-family: Monaco; background-color: rgb(255, 255, 255);">子類在執行到onResume方法的時候調用父類的onResume方法,此時方法initTopLayout中的參數爲子類的實例在方法initTopLayout中就可以得到子類佈局中的各個控件。</span>
public void initTopLayout(Activity act) {
RelativeLayout rlTop = (RelativeLayout) act.findViewById(R.id.base_top);
if (rlTop == null) {
throw new RuntimeException("please inlucde layout common_title in Ur layout,and make sure the include id is @+id/common_title @+id/base_top");
}
this.leftBtn = (Button) rlTop.findViewById(R.id.btn_globaltop_leftbutton);
this.titleTv = (TextView) rlTop.findViewById(R.id.tv_globaltop_title);
this.rightTv = (TextView) rlTop.findViewById(R.id.tv_globaltop_righttext);
this.rightIv = (ImageView) rlTop.findViewById(R.id.iv_globaltop_rightiv);
}
在BaseActivity的onResume方法中的setTop(leftBtn, titleTv, rightTv, rightIv);爲對各個或得到的控件屬性進行設置的方法。
public void setTop(Button leftBtn, TextView titleTv, TextView rightTv, ImageView rightIv) {
super.setTop(leftBtn, titleTv, rightTv, rightIv);
enableBackBtn();
rightIv.setVisibility(View.VISIBLE);
rightIv.setImageResource(R.drawable.ibtn_selector_share);
rightIv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO:share
}
});
}
同理的還有BaseFragment等的應用,也可以參照這種方法實現精簡代碼結構。