http://blog.csdn.net/jiahui524

来源:互联网 发布:端游 页游区别 知乎 编辑:程序博客网 时间:2024/06/10 05:42
为你的Android应用定制属于你的BaseActivity

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

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

 

先上效果图

                

 

 

 

效果图看了,大家是否有所启发或是有所了解呢?那么接下来就放BaseActivity里的核心代码咯:

[java] view plaincopyprint?
  1. /** 
  2.  * 继承于Activity用于以后方便管理 
  3.  *  
  4.  * @author coder 
  5.  *  
  6.  */  
  7. public class BaseActivity extends Activity {  
  8.   
  9.     private View titleView;  
  10.     private TextView tv_title;  
  11.     private Button btn_left, btn_right;  
  12.   
  13.     private LinearLayout ly_content;  
  14.     // 内容区域的布局  
  15.     private View contentView;  
  16.   
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         // TODO Auto-generated method stub  
  20.         super.onCreate(savedInstanceState);  
  21.   
  22.         setContentView(R.layout.common_title);  
  23.         titleView = findViewById(R.id.titleView);  
  24.         tv_title = (TextView) titleView.findViewById(R.id.tv_title);  
  25.         btn_left = (Button) titleView.findViewById(R.id.btn_left);  
  26.         btn_right = (Button) titleView.findViewById(R.id.btn_right);  
  27.   
  28.         ly_content = (LinearLayout) findViewById(R.id.ly_content);  
  29.     }  
  30.   
  31.     /*** 
  32.      * 设置内容区域 
  33.      *  
  34.      * @param resId 
  35.      *            资源文件ID 
  36.      */  
  37.     public void setContentLayout(int resId) {  
  38.   
  39.         LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  40.         contentView = inflater.inflate(resId, null);  
  41.         LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT,  
  42.                 LayoutParams.FILL_PARENT);  
  43.         contentView.setLayoutParams(layoutParams);  
  44.         contentView.setBackgroundDrawable(null);  
  45.         if (null != ly_content) {  
  46.             ly_content.addView(contentView);  
  47.         }  
  48.   
  49.     }  
  50.   
  51.     /*** 
  52.      * 设置内容区域 
  53.      *  
  54.      * @param view 
  55.      *            View对象 
  56.      */  
  57.     public void setContentLayout(View view) {  
  58.         if (null != ly_content) {  
  59.             ly_content.addView(view);  
  60.         }  
  61.     }  
  62.   
  63.     /** 
  64.      * 得到内容的View 
  65.      *  
  66.      * @return 
  67.      */  
  68.     public View getLyContentView() {  
  69.   
  70.         return contentView;  
  71.     }  
  72.   
  73.     /** 
  74.      * 得到左边的按钮 
  75.      *  
  76.      * @return 
  77.      */  
  78.     public Button getbtn_left() {  
  79.         return btn_left;  
  80.     }  
  81.   
  82.     /** 
  83.      * 得到右边的按钮 
  84.      *  
  85.      * @return 
  86.      */  
  87.     public Button getbtn_right() {  
  88.         return btn_right;  
  89.     }  
  90.   
  91.     /** 
  92.      * 设置标题 
  93.      *  
  94.      * @param title 
  95.      */  
  96.     public void setTitle(String title) {  
  97.   
  98.         if (null != tv_title) {  
  99.             tv_title.setText(title);  
  100.         }  
  101.   
  102.     }  
  103.   
  104.     /** 
  105.      * 设置标题 
  106.      *  
  107.      * @param resId 
  108.      */  
  109.     public void setTitle(int resId) {  
  110.         tv_title.setText(getString(resId));  
  111.     }  
  112.   
  113.     /** 
  114.      * 设置左边按钮的图片资源 
  115.      *  
  116.      * @param resId 
  117.      */  
  118.     public void setbtn_leftRes(int resId) {  
  119.         if (null != btn_left) {  
  120.             btn_left.setBackgroundResource(resId);  
  121.         }  
  122.   
  123.     }  
  124.   
  125.     /** 
  126.      * 设置左边按钮的图片资源 
  127.      *  
  128.      * @param bm 
  129.      */  
  130.     public void setbtn_leftRes(Drawable drawable) {  
  131.         if (null != btn_left) {  
  132.             btn_left.setBackgroundDrawable(drawable);  
  133.         }  
  134.   
  135.     }  
  136.   
  137.     /** 
  138.      * 设置右边按钮的图片资源 
  139.      *  
  140.      * @param resId 
  141.      */  
  142.     public void setbtn_rightRes(int resId) {  
  143.         if (null != btn_right) {  
  144.             btn_right.setBackgroundResource(resId);  
  145.         }  
  146.     }  
  147.   
  148.     /** 
  149.      * 设置右边按钮的图片资源 
  150.      *  
  151.      * @param drawable 
  152.      */  
  153.     public void setbtn_rightRes(Drawable drawable) {  
  154.         if (null != btn_right) {  
  155.             btn_right.setBackgroundDrawable(drawable);  
  156.         }  
  157.     }  
  158.   
  159.     /** 
  160.      * 隐藏上方的标题栏 
  161.      */  
  162.     public void hideTitleView() {  
  163.   
  164.         if (null != titleView) {  
  165.             titleView.setVisibility(View.GONE);  
  166.         }  
  167.     }  
  168.   
  169.     /** 
  170.      * 隐藏左边的按钮 
  171.      */  
  172.     public void hidebtn_left() {  
  173.   
  174.         if (null != btn_left) {  
  175.             btn_left.setVisibility(View.GONE);  
  176.         }  
  177.   
  178.     }  
  179.   
  180.     /*** 
  181.      * 隐藏右边的按钮 
  182.      */  
  183.     public void hidebtn_right() {  
  184.         if (null != btn_right) {  
  185.             btn_right.setVisibility(View.GONE);  
  186.         }  
  187.   
  188.     }  
  189.   
  190.     public BaseActivity() {  
  191.   
  192.     }  
  193.   
  194. }  

接下来再给出其中的一个用法就可以了:

[java] view plaincopyprint?
  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.   
  7.         setContentLayout(R.layout.two);  
  8.   
  9.         //设置标题  
  10.         setTitle("两个按钮");  
  11.   
  12.   
  13.   
  14.         // 为左边的按钮增加监听事件  
  15.         getbtn_left().setOnClickListener(new OnClickListener() {  
  16.   
  17.             @Override  
  18.             public void onClick(View v) {  
  19.                 onBackPressed();  
  20.   
  21.             }  
  22.         });  
  23.   
  24.     }  
  25.   
  26. }  


好了大功告成了,这个万能的BaseActivity是不是很好用呀,希望这样的一个小小的分享能对大家有所帮助咯

0 0
原创粉丝点击