/此文适用于初级开发者/
在我们刚开始做项目开发的时候,会发现有很多冗余代码,也就是说同一份代码块我们可能再次基础上修改下然后应用于另外的页面。甚至有一些代码都不用做任何修改。这个时候Base基类就派上了用场,利用java继承这一特性,我们可以把共用的代码模板都写到一个类里,这样就能大大减少我们的开发时间并且使代码更加简洁高效。
举个例子:我们app内的多个Activity可能需要的标题栏,返回按钮都是一个样式的,这样我们不可能每个页面都复制一份相同的标题栏代码吧,我们可以在我们的baseActivity中写好我们的样式,然后只需要一行代码就可以引用过来,并可以在此基础上进行修改以及添加点击事件等。下面就是代码演示:
BaseActivity
public class ActBase extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//透明状态栏
setContentView(R.layout.app_template_page);
}
@Override
protected void onResume() {
//可以做友盟统计
super.onResume();
}
/**
* @param titleText 标题
* @param leftBtnListener 左边按钮点击事件, 默认为返回按钮
*/
protected void setTitleBarAndAction(@StringRes int titleText, OnClickListener leftBtnListener) {
setTitleBarAndAction(titleText, leftBtnListener, null);
}
/**
* @param titleText 标题
* @param leftBtnListener 左边按钮点击事件, 默认为返回按钮
* @param subRightBtnListener 右边按钮点击事件
*/
protected void setTitleBarAndAction(@StringRes int titleText, OnClickListener leftBtnListener, OnClickListener subRightBtnListener) {
setTitleBarAndAction(titleText, R.drawable.app_title_back_selector, leftBtnListener, 0, subRightBtnListener);
}
/**
* @param titleText 标题
* @param leftResource 左边图片资源
* @param leftBtnListener 左边按钮点击事件
* @param subRightResource 右边图片资源
* @param subRightBtnListener 右边按钮点击事件
*/
protected void setTitleBarAndAction(@StringRes int titleText,
@DrawableRes int leftResource, OnClickListener leftBtnListener,
@DrawableRes int subRightResource, OnClickListener subRightBtnListener) {
setTitleBarAndAction(titleText, leftResource, 0, 0, leftBtnListener, subRightResource, 0, 0, subRightBtnListener);
}
/**
* @param titleText 标题
* @param leftResource 左边图片资源
* @param leftResourceTextColor 左边文字颜色
* @param leftText 左边文字
* @param leftBtnListener 左边按钮点击事件
* @param subRightResource 右边图片资源
* @param subRightResourceTextColor 右边文字颜色
* @param subRightText 右边文字
* @param subRightBtnListener 右边按钮点击事件
*/
protected void setTitleBarAndAction(@StringRes int titleText,
@DrawableRes int leftResource, @ColorRes int leftResourceTextColor, @StringRes int leftText, OnClickListener leftBtnListener,
@DrawableRes int subRightResource, @ColorRes int subRightResourceTextColor, @StringRes int subRightText, OnClickListener subRightBtnListener) {
setTitleBarAndAction(titleText, leftResource, leftResourceTextColor, leftText, leftBtnListener,
subRightResource, subRightResourceTextColor, subRightText, subRightBtnListener,
0, 0, 0, new OnClickListener() {
@Override
public void onClick(View v) {
//点击事件
}
});
}
/**
* @param titleText 标题
* @param leftBtnListener 左边按钮点击事件
* @param subRightResource 右边图片资源
* @param subRightResourceTextColor 右边文字颜色
* @param subRightText 右边文字
* @param subRightBtnListener 右边按钮点击事件
*/
protected void setTitleBarAndAction(@StringRes int titleText,
OnClickListener leftBtnListener,
@DrawableRes int subRightResource, @ColorRes int subRightResourceTextColor, @StringRes int subRightText, OnClickListener subRightBtnListener) {
setTitleBarAndAction(titleText, leftBtnListener,
subRightResource, subRightResourceTextColor, subRightText, subRightBtnListener,
new OnClickListener() {
@Override
public void onClick(View v) {
//点击事件
}
});
}
/**
* @param titleText 标题
* @param leftBtnListener 左边按钮点击事件
* @param subRightResource 右边图片资源
* @param subRightResourceTextColor 右边文字颜色
* @param subRightText 右边文字
* @param subRightBtnListener 右边按钮点击事件
* @param rightBtnListener 最右侧按钮点击事件
*/
protected void setTitleBarAndAction(@StringRes int titleText,
OnClickListener leftBtnListener,
@DrawableRes int subRightResource, @ColorRes int subRightResourceTextColor, @StringRes int subRightText, OnClickListener subRightBtnListener,
OnClickListener rightBtnListener) {
setTitleBarAndAction(titleText, 0, 0, 0, leftBtnListener,
subRightResource, subRightResourceTextColor, subRightText, subRightBtnListener,
0, 0, 0, rightBtnListener);
}
/**
* @param titleText 标题
* @param leftResource 左边图片资源
* @param leftResourceTextColor 左边文字颜色
* @param leftText 左边文字
* @param leftBtnListener 左边按钮点击事件
* @param subRightResource 右边图片资源
* @param subRightResourceTextColor 右边文字颜色
* @param subRightText 右边文字
* @param subRightBtnListener 右边按钮点击事件
* @param rightResource 最右侧按钮图片资源
* @param rightResourceTextColor 最右侧按钮文字颜色
* @param rightText 最右侧按钮文字
* @param rightBtnListener 最右侧按钮点击事件
*/
protected void setTitleBarAndAction(@StringRes int titleText,
@DrawableRes int leftResource, @ColorRes int leftResourceTextColor, @StringRes int leftText, OnClickListener leftBtnListener,
@DrawableRes int subRightResource, @ColorRes int subRightResourceTextColor, @StringRes int subRightText, OnClickListener subRightBtnListener,
@DrawableRes int rightResource, @ColorRes int rightResourceTextColor, @StringRes int rightText, OnClickListener rightBtnListener) {
alabSetBarTitleText(titleText);
setLeftBtnAction(leftResource, leftResourceTextColor, leftText, leftBtnListener);
setSubRightBtnAction(subRightResource, subRightResourceTextColor, subRightText, subRightBtnListener);
setRightBtnAction(rightResource, rightResourceTextColor, rightText, rightBtnListener);
}
private void setLeftBtnAction(@DrawableRes int leftResource, @ColorRes int leftResourceTextColor,
@StringRes int leftText, OnClickListener leftBtnListener) {
TextView leftBtn = alabGetButtonLeft();
if (0 != leftResource) {
leftBtn.setCompoundDrawablesWithIntrinsicBounds(leftResource, 0, 0, 0);
}
if (0 != leftResourceTextColor) {
leftBtn.setTextColor(ContextCompat.getColor(this, leftResourceTextColor));
}
if (0 != leftText) {
leftBtn.setText(leftText);
}
if (null != leftBtnListener) {
leftBtn.setVisibility(View.VISIBLE);
leftBtn.setOnClickListener(leftBtnListener);
}
}
protected void setRightBtnAction(@DrawableRes int rightResource, @ColorRes int rightResourceTextColor,
@StringRes int rightText, OnClickListener rightBtnListener) {
TextView rightBtn = alabGetButtonRight();
if (0 != rightResource || 0 != rightText) {
rightBtn.setCompoundDrawablesWithIntrinsicBounds(rightResource, 0, 0, 0);
rightBtn.setCompoundDrawablePadding(3);
if (0 != rightText) {
rightBtn.setText(rightText);
}
}
if (0 != rightResourceTextColor) {
rightBtn.setTextColor(ContextCompat.getColor(this, rightResourceTextColor));
}
if (null != rightBtnListener) {
rightBtn.setVisibility(View.VISIBLE);
rightBtn.setOnClickListener(rightBtnListener);
} else {
rightBtn.setVisibility(View.INVISIBLE);
rightBtn.setOnClickListener(null);
}
}
protected void setSubRightBtnAction(@DrawableRes int subRightResource, @ColorRes int subRightResourceTextColor,
@StringRes int subRightText, OnClickListener subRightBtnListener) {
TextView subRightBtn = alabGetButtonSubRight();
if (0 != subRightResource) {
subRightBtn.setCompoundDrawablesWithIntrinsicBounds(subRightResource, 0, 0, 0);
}
if (0 != subRightResourceTextColor) {
subRightBtn.setTextColor(ContextCompat.getColor(this, subRightResourceTextColor));
}
if (0 != subRightText) {
subRightBtn.setText(subRightText);
}
if (null != subRightBtnListener) {
subRightBtn.setVisibility(View.VISIBLE);
subRightBtn.setOnClickListener(subRightBtnListener);
}
}
private TextView alabGetButtonLeft() {
return (TextView) findViewById(R.id.title_back);
}
protected TextView alabGetButtonRight() {
return (TextView) findViewById(R.id.title_right);
}
private TextView alabGetButtonSubRight() {
return (TextView) findViewById(R.id.title_sub_right);
}
/**
* Title: hideTitleBar
* Description: 隐藏标题栏
*/
protected void hideTitleBar() {
RelativeLayout titleBar = (RelativeLayout) findViewById(R.id.id_title_bar);
titleBar.setVisibility(View.GONE);
}
/**
* Title: showTitleDivider
* Description: 显示标题栏的水平分割线
*/
protected void showTitleDivider() {
ImageView titleDivider = (ImageView) findViewById(R.id.id_title_divider);
titleDivider.setVisibility(View.VISIBLE);
}
protected String getResourceStr(int id) {
return getResources().getString(id);
}
/**
* 设置主内容区域的View
*
* @param v view
*/
protected void setContentField(View v) {
/* 底层 */
LinearLayout llContent = (LinearLayout) findViewById(R.id.llContent1);
llContent.removeAllViews();
llContent.addView(v);
v.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
ButterKnife.bind(this);
initData();
findView(v);
setOnClickEvent();
}
/**
* 设置主内容区域的layoutRes
*
* @param layoutResId layoutid
*/
protected void setContentField(int layoutResId) {
/* 底层 */
LinearLayout llContent = (LinearLayout) findViewById(R.id.llContent1);
llContent.removeAllViews();
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(layoutResId, null);
llContent.addView(v);
v.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
ButterKnife.bind(this);
initData();
findView(v);
setOnClickEvent();
}
/**
* Title: initData
* Description: 初始化数据, 在加载view以前
*/
protected void initData() {
}
/**
* Title: findView
* Description: 加载view
*
* @param v view
*/
protected void findView(View v) {
}
/**
* Title: setOnClickEvent
* Description: 设置button点击事件
*/
protected void setOnClickEvent() {
}
protected void alabSetBarTitleText(String titleText) {
TextView tv = (TextView) findViewById(R.id.title_txt);
if (null != tv) {
tv.setText(titleText);
}
}
protected void alabSetBarTitleText(@StringRes int resourceId) {
TextView tv = (TextView) findViewById(R.id.title_txt);
if (null != tv && 0 != resourceId) {
tv.setText(resourceId);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
BaseLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- 标题栏 -->
<RelativeLayout
android:id="@+id/id_title_bar"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#FFFFFF">
<!-- 返回按钮 -->
<TextView
android:id="@+id/title_back"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:background="@null"
android:drawableLeft="@drawable/app_title_back_selector"
android:gravity="center_vertical"
android:paddingLeft="12dp"
android:paddingRight="7dp"
android:singleLine="true"
android:textColor="#333333"
android:textSize="16dp"
android:visibility="invisible"/>
<!-- 标题 -->
<TextView
android:id="@+id/title_txt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingLeft="80dp"
android:paddingRight="80dp"
android:singleLine="true"
android:textColor="#333333"
android:textSize="18dp"/>
<!-- 首页按钮 -->
<TextView
android:id="@+id/title_right"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:background="@null"
android:drawableLeft="@mipmap/shortcut_more"
android:gravity="center_vertical"
android:paddingRight="12dp"
android:textColor="#333333"
android:textSize="16dp"
android:visibility="invisible"/>
<TextView
android:id="@+id/title_sub_right"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/title_right"
android:background="@null"
android:gravity="center_vertical"
android:textColor="#333333"
android:textSize="16dp"
android:visibility="invisible"/>
<ImageView
android:id="@+id/id_title_divider"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_alignParentBottom="true"
android:background="#666666"
android:visibility="gone"/>
</RelativeLayout>
<!-- 底部按钮 -->
<LinearLayout
android:id="@+id/layout_live_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#333333"
android:gravity="center_vertical"
android:visibility="gone">
<TextView
android:id="@+id/tv_comment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawableLeft="@drawable/app_title_back_selector"
android:gravity="center_vertical"
android:textColor="#333333"
android:textSize="12dp"
android:visibility="gone"/>
<ImageView
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:background="#333333"/>
<ImageView
android:id="@+id/iv_share"
android:layout_width="44dp"
android:layout_height="44dp"
android:layout_alignParentRight="true"
android:background="@drawable/app_title_share_selector"
android:visibility="gone"/>
</LinearLayout>
<!-- 主内容框架 -->
<LinearLayout
android:id="@+id/llContent1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/layout_live_info"
android:layout_below="@id/id_title_bar"
android:orientation="vertical"/>
</RelativeLayout>
MainActivity
public class MainActivity extends ActBase {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentField(getLayoutInflater().inflate(R.layout.activity_main, null));
setTitleBarAndAction(R.string.title, R.mipmap.ic_launcher, R.color.colorAccent, R.string.left_text, new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "左边按钮的点击事件", Toast.LENGTH_SHORT).show();
}
}, R.mipmap.ic_launcher, R.color.colorAccent, R.string.right_text, new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "右边按钮的点击事件", Toast.LENGTH_SHORT).show();
}
}, R.mipmap.ic_launcher, R.color.colorAccent, R.string.very_right_text, new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "最右按钮的点击事件", Toast.LENGTH_SHORT).show();
}
});
}
}
接下来我们可以看下效果:
好了,从此以后我们就可以舍弃复制粘贴的操作了,是不是省下了好多重复工作呢.至于BaseFragment 的编写的思路大体相同,这里就不贴了.
预告一下下一篇文章--建立属于你的Activity管理栈之ActManger的建立