相信大家在开发Android应用的过程中肯定碰到过很多重复的工作,写着重复的代码,有时候连布局文件也是一样,需要重复的劳动,那么这样对于我们程序来讲肯定是很累很繁琐的一件事,所以我们在写代码的时候是否需要去考虑让我们写更少的代码,程序员要学会偷懒,否则……..
在开发应用程序的时候我们的设计其实整体的样式是统一,那么我们就可以写一些公用的代码,这样对程序来讲也便于后面的维护,废话也不多说了,相信大家肯定也懂的,今天我分享给大家的就是定制一个属于自己的BaseActivity,这个BaseActivity主要封装了一些公用的代码,例如我们在开发过程中上面的那些标题和按钮肯定都要有的,所以我们可以把这些公用的都写在这个BaseActivity里,其他功能的Activity以后都继承这个BaseActivity.
先上效果图
效果图看了,大家是否有所启发或是有所了解呢?那么接下来就放BaseActivity里的核心代码咯:
- /**
- * 继承于Activity用于以后方便管理
- *
- * @author coder
- *
- */
- public class BaseActivity extends Activity {
- private View titleView;
- private TextView tv_title;
- private Button btn_left, btn_right;
- private LinearLayout ly_content;
- // 内容区域的布局
- private View contentView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.common_title);
- titleView = findViewById(R.id.titleView);
- tv_title = (TextView) titleView.findViewById(R.id.tv_title);
- btn_left = (Button) titleView.findViewById(R.id.btn_left);
- btn_right = (Button) titleView.findViewById(R.id.btn_right);
- ly_content = (LinearLayout) findViewById(R.id.ly_content);
- }
- /***
- * 设置内容区域
- *
- * @param resId
- * 资源文件ID
- */
- public void setContentLayout(int resId) {
- LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- contentView = inflater.inflate(resId, null);
- LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT,
- LayoutParams.FILL_PARENT);
- contentView.setLayoutParams(layoutParams);
- contentView.setBackgroundDrawable(null);
- if (null != ly_content) {
- ly_content.addView(contentView);
- }
- }
- /***
- * 设置内容区域
- *
- * @param view
- * View对象
- */
- public void setContentLayout(View view) {
- if (null != ly_content) {
- ly_content.addView(view);
- }
- }
- /**
- * 得到内容的View
- *
- * @return
- */
- public View getLyContentView() {
- return contentView;
- }
- /**
- * 得到左边的按钮
- *
- * @return
- */
- public Button getbtn_left() {
- return btn_left;
- }
- /**
- * 得到右边的按钮
- *
- * @return
- */
- public Button getbtn_right() {
- return btn_right;
- }
- /**
- * 设置标题
- *
- * @param title
- */
- public void setTitle(String title) {
- if (null != tv_title) {
- tv_title.setText(title);
- }
- }
- /**
- * 设置标题
- *
- * @param resId
- */
- public void setTitle(int resId) {
- tv_title.setText(getString(resId));
- }
- /**
- * 设置左边按钮的图片资源
- *
- * @param resId
- */
- public void setbtn_leftRes(int resId) {
- if (null != btn_left) {
- btn_left.setBackgroundResource(resId);
- }
- }
- /**
- * 设置左边按钮的图片资源
- *
- * @param bm
- */
- public void setbtn_leftRes(Drawable drawable) {
- if (null != btn_left) {
- btn_left.setBackgroundDrawable(drawable);
- }
- }
- /**
- * 设置右边按钮的图片资源
- *
- * @param resId
- */
- public void setbtn_rightRes(int resId) {
- if (null != btn_right) {
- btn_right.setBackgroundResource(resId);
- }
- }
- /**
- * 设置右边按钮的图片资源
- *
- * @param drawable
- */
- public void setbtn_rightRes(Drawable drawable) {
- if (null != btn_right) {
- btn_right.setBackgroundDrawable(drawable);
- }
- }
- /**
- * 隐藏上方的标题栏
- */
- public void hideTitleView() {
- if (null != titleView) {
- titleView.setVisibility(View.GONE);
- }
- }
- /**
- * 隐藏左边的按钮
- */
- public void hidebtn_left() {
- if (null != btn_left) {
- btn_left.setVisibility(View.GONE);
- }
- }
- /***
- * 隐藏右边的按钮
- */
- public void hidebtn_right() {
- if (null != btn_right) {
- btn_right.setVisibility(View.GONE);
- }
- }
- public BaseActivity() {
- }
- }
接下来再给出其中的一个用法就可以了:
- public class TwoBtnActivity extends BaseActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentLayout(R.layout.two);
- //设置标题
- setTitle("两个按钮");
- // 为左边的按钮增加监听事件
- getbtn_left().setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- onBackPressed();
- }
- });
- }
- }
好了大功告成了,这个万能的BaseActivity是不是很好用呀,希望这样的一个小小的分享能对大家有所帮助咯