protected void onResume() { <span style="white-space:pre"> </span>super.onResume(); <span style="white-space:pre"> </span>initTopLayout(this); <span style="white-space:pre"> </span>Log.i("BaseActivity", "onResume"); <span style="white-space:pre"> </span>setTop(leftBtn, titleTv, rightTv, rightIv); <span style="white-space:pre"> </span>}子类在执行到onResume方法的时候调用父类的onResume方法,此时方法initTopLayout中的参数为子类的实例在方法initTopLayout中就可以得到子类布局中的各个控件。
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); }
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等的应用,也可以参照这种方法实现精简代码结构。
<include>标签和BaseActivity联合使用简化代码结构
原文地址:http://blog.csdn.net/u012293381/article/details/40744789