定制属于你的BaseActivity

相信大家在开发Android应用的过程中肯定碰到过很多重复的工作,写着重复的代码,有时候连布局文件也是一样,需要重复的劳动,那么这样对于我们程序来讲肯定是很累很繁琐的一件事,所以我们在写代码的时候是否需要去考虑让我们写更少的代码,程序员要学会偷懒,否则……..

 

在开发应用程序的时候我们的设计其实整体的样式是统一,那么我们就可以写一些公用的代码,这样对程序来讲也便于后面的维护,废话也不多说了,相信大家肯定也懂的,今天我分享给大家的就是定制一个属于自己的BaseActivity,这个BaseActivity主要封装了一些公用的代码,例如我们在开发过程中上面的那些标题和按钮肯定都要有的,所以我们可以把这些公用的都写在这个BaseActivity,其他功能的Activity以后都继承这个BaseActivity.
 
大家是否有所启发或是有所了解呢?那么接下来就放BaseActivity里的核心代码咯:
  1. /**
  2. * 继承于Activity用于以后方便管理
  3. *
  4. * @author coder
  5. *
  6. */
  7. public class BaseActivity extends Activity {
  8. private View titleView;
  9. private TextView tv_title;
  10. private Button btn_left, btn_right;
  11. private LinearLayout ly_content;
  12. // 内容区域的布局
  13. private View contentView;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. // TODO Auto-generated method stub
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.common_title);
  19. titleView = findViewById(R.id.titleView);
  20. tv_title = (TextView) titleView.findViewById(R.id.tv_title);
  21. btn_left = (Button) titleView.findViewById(R.id.btn_left);
  22. btn_right = (Button) titleView.findViewById(R.id.btn_right);
  23. ly_content = (LinearLayout) findViewById(R.id.ly_content);
  24. }
  25. /***
  26. * 设置内容区域
  27. *
  28. * @param resId
  29. * 资源文件ID
  30. */
  31. public void setContentLayout(int resId) {
  32. LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  33. contentView = inflater.inflate(resId, null);
  34. LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT,
  35. LayoutParams.FILL_PARENT);
  36. contentView.setLayoutParams(layoutParams);
  37. contentView.setBackgroundDrawable(null);
  38. if (null != ly_content) {
  39. ly_content.addView(contentView);
  40. }
  41. }
  42. /***
  43. * 设置内容区域
  44. *
  45. * @param view
  46. * View对象
  47. */
  48. public void setContentLayout(View view) {
  49. if (null != ly_content) {
  50. ly_content.addView(view);
  51. }
  52. }
  53. /**
  54. * 得到内容的View
  55. *
  56. * @return
  57. */
  58. public View getLyContentView() {
  59. return contentView;
  60. }
  61. /**
  62. * 得到左边的按钮
  63. *
  64. * @return
  65. */
  66. public Button getbtn_left() {
  67. return btn_left;
  68. }
  69. /**
  70. * 得到右边的按钮
  71. *
  72. * @return
  73. */
  74. public Button getbtn_right() {
  75. return btn_right;
  76. }
  77. /**
  78. * 设置标题
  79. *
  80. * @param title
  81. */
  82. public void setTitle(String title) {
  83. if (null != tv_title) {
  84. tv_title.setText(title);
  85. }
  86. }
  87. /**
  88. * 设置标题
  89. *
  90. * @param resId
  91. */
  92. public void setTitle(int resId) {
  93. tv_title.setText(getString(resId));
  94. }
  95. /**
  96. * 设置左边按钮的图片资源
  97. *
  98. * @param resId
  99. */
  100. public void setbtn_leftRes(int resId) {
  101. if (null != btn_left) {
  102. btn_left.setBackgroundResource(resId);
  103. }
  104. }
  105. /**
  106. * 设置左边按钮的图片资源
  107. *
  108. * @param bm
  109. */
  110. public void setbtn_leftRes(Drawable drawable) {
  111. if (null != btn_left) {
  112. btn_left.setBackgroundDrawable(drawable);
  113. }
  114. }
  115. /**
  116. * 设置右边按钮的图片资源
  117. *
  118. * @param resId
  119. */
  120. public void setbtn_rightRes(int resId) {
  121. if (null != btn_right) {
  122. btn_right.setBackgroundResource(resId);
  123. }
  124. }
  125. /**
  126. * 设置右边按钮的图片资源
  127. *
  128. * @param drawable
  129. */
  130. public void setbtn_rightRes(Drawable drawable) {
  131. if (null != btn_right) {
  132. btn_right.setBackgroundDrawable(drawable);
  133. }
  134. }
  135. /**
  136. * 隐藏上方的标题栏
  137. */
  138. public void hideTitleView() {
  139. if (null != titleView) {
  140. titleView.setVisibility(View.GONE);
  141. }
  142. }
  143. /**
  144. * 隐藏左边的按钮
  145. */
  146. public void hidebtn_left() {
  147. if (null != btn_left) {
  148. btn_left.setVisibility(View.GONE);
  149. }
  150. }
  151. /***
  152. * 隐藏右边的按钮
  153. */
  154. public void hidebtn_right() {
  155. if (null != btn_right) {
  156. btn_right.setVisibility(View.GONE);
  157. }
  158. }
  159. public BaseActivity() {
  160. }
  161. }
复制代码
 
 
接下来再给出其中的一个用法就可以了:
  1. public class TwoBtnActivity extends BaseActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. // TODO Auto-generated method stub
  5. super.onCreate(savedInstanceState);
  6. setContentLayout(R.layout.two);
  7. //设置标题
  8. setTitle("两个按钮");
  9. // 为左边的按钮增加监听事件
  10. getbtn_left().setOnClickListener(new OnClickListener() {
  11. @Override
  12. public void onClick(View v) {
  13. onBackPressed();
  14. }
  15. });
  16. }
  17. }
  18. 好了大功告成了,这个万能的BaseActivity是不是很好用呀,希望这样的一个小小的分享能对大家有所帮助咯
posted @ 2012-08-20 12:50  囧里个囧  阅读(359)  评论(0)    收藏  举报