你的Android应用定制属于你的BaseActivity

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

在开发应用程序的时候我们的设计其实整体的样式是统一, 那么我们就可以写一些公用的代码, 这样对程序来讲也便于后面的维护, 废话也不多说了, 相信大家肯定也懂的, 今天我分享给大家的就是定制一个属于自己的BaseActivity, 这个BaseActivity 主要封装了一些公用的代码, 例如我们在开发过程中上面的那些标题和按钮肯定都要有的, 所以我们可以把这些公用的都写在这个BaseActivity , 其他功能的Activity 以后都继承这个BaseActivity.
先上效果图

  
效果图看了, 大家是否有所启发或是有所了解呢? 那么接下来就放BaseActivity 里的核心代码咯:
  1. /**
  2. * 继承于Activity用于以后方便管理

  3. * @author coder

  4. */
  5. public class BaseActivity extends Activity {

  6.         private View titleView;
  7.         private TextView tv_title;
  8.         private Button btn_left, btn_right;

  9.         private LinearLayout ly_content;
  10.         // 内容区域的布局
  11.         private View contentView;

  12.         @Override
  13.         protected void onCreate(Bundle savedInstanceState) {
  14.                 // TODO Auto-generated method stub
  15.                 super.onCreate(savedInstanceState);

  16.                 setContentView(R.layout.common_title);
  17.                 titleView = findViewById(R.id.titleView);
  18.                 tv_title = (TextView) titleView.findViewById(R.id.tv_title);
  19.                 btn_left = (Button) titleView.findViewById(R.id.btn_left);
  20.                 btn_right = (Button) titleView.findViewById(R.id.btn_right);

  21.                 ly_content = (LinearLayout) findViewById(R.id.ly_content);
  22.         }

  23.         /***
  24.          * 设置内容区域
  25.          * 
  26.          * @param resId
  27.          *            资源文件ID
  28.          */
  29.         public void setContentLayout(int resId) {

  30.                 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  31.                 contentView = inflater.inflate(resId, null);
  32.                 LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT,
  33.                                 LayoutParams.FILL_PARENT);
  34.                 contentView.setLayoutParams(layoutParams);
  35.                 contentView.setBackgroundDrawable(null);
  36.                 if (null != ly_content) {
  37.                         ly_content.addView(contentView);
  38.                 }

  39.         }

  40.         /***
  41.          * 设置内容区域
  42.          * 
  43.          * @param view
  44.          *            View对象
  45.          */
  46.         public void setContentLayout(View view) {
  47.                 if (null != ly_content) {
  48.                         ly_content.addView(view);
  49.                 }
  50.         }

  51.         /**
  52.          * 得到内容的View
  53.          * 
  54.          * @return
  55.          */
  56.         public View getLyContentView() {

  57.                 return contentView;
  58.         }

  59.         /**
  60.          * 得到左边的按钮
  61.          * 
  62.          * @return
  63.          */
  64.         public Button getbtn_left() {
  65.                 return btn_left;
  66.         }

  67.         /**
  68.          * 得到右边的按钮
  69.          * 
  70.          * @return
  71.          */
  72.         public Button getbtn_right() {
  73.                 return btn_right;
  74.         }

  75.         /**
  76.          * 设置标题
  77.          * 
  78.          * @param title
  79.          */
  80.         public void setTitle(String title) {

  81.                 if (null != tv_title) {
  82.                         tv_title.setText(title);
  83.                 }

  84.         }

  85.         /**
  86.          * 设置标题
  87.          * 
  88.          * @param resId
  89.          */
  90.         public void setTitle(int resId) {
  91.                 tv_title.setText(getString(resId));
  92.         }

  93.         /**
  94.          * 设置左边按钮的图片资源
  95.          * 
  96.          * @param resId
  97.          */
  98.         public void setbtn_leftRes(int resId) {
  99.                 if (null != btn_left) {
  100.                         btn_left.setBackgroundResource(resId);
  101.                 }

  102.         }

  103.         /**
  104.          * 设置左边按钮的图片资源
  105.          * 
  106.          * @param bm
  107.          */
  108.         public void setbtn_leftRes(Drawable drawable) {
  109.                 if (null != btn_left) {
  110.                         btn_left.setBackgroundDrawable(drawable);
  111.                 }

  112.         }

  113.         /**
  114.          * 设置右边按钮的图片资源
  115.          * 
  116.          * @param resId
  117.          */
  118.         public void setbtn_rightRes(int resId) {
  119.                 if (null != btn_right) {
  120.                         btn_right.setBackgroundResource(resId);
  121.                 }
  122.         }

  123.         /**
  124.          * 设置右边按钮的图片资源
  125.          * 
  126.          * @param drawable
  127.          */
  128.         public void setbtn_rightRes(Drawable drawable) {
  129.                 if (null != btn_right) {
  130.                         btn_right.setBackgroundDrawable(drawable);
  131.                 }
  132.         }

  133.         /**
  134.          * 隐藏上方的标题栏
  135.          */
  136.         public void hideTitleView() {

  137.                 if (null != titleView) {
  138.                         titleView.setVisibility(View.GONE);
  139.                 }
  140.         }

  141.         /**
  142.          * 隐藏左边的按钮
  143.          */
  144.         public void hidebtn_left() {

  145.                 if (null != btn_left) {
  146.                         btn_left.setVisibility(View.GONE);
  147.                 }

  148.         }

  149.         /***
  150.          * 隐藏右边的按钮
  151.          */
  152.         public void hidebtn_right() {
  153.                 if (null != btn_right) {
  154.                         btn_right.setVisibility(View.GONE);
  155.                 }

  156.         }

  157.         public BaseActivity() {

  158.         }

  159. }
复制代码
接下来再给出其中的一个用法就可以了:

  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. }
复制代码
好了大功告成了, 这个万能的BaseActivity 是不是很好用呀, 希望这样的一个小小的分享能对大家有所帮助咯
更多精彩博文:http://blog.csdn.net/jiahui524  欢迎共同交流
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值