现在的位置: 首页 > 综合 > 正文

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

2013年03月14日 ⁄ 综合 ⁄ 共 2935字 ⁄ 字号 评论关闭

转载出处:http://blog.csdn.net/jiahui524

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

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

 

先上效果图

                

 

 

 

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

  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.      * 设置左边按钮的图片资源 

抱歉!评论已关闭.