为你的Android应用定制属于你的BaseActivity http://blog.csdn.net/jiahui524
2016-02-26 01:21
501 查看
为你的Android应用定制属于你的BaseActivity
2013年08月28日 ⁄ 综合 ⁄ 共 3297字 ⁄ 字号 小 中 大 ⁄ 评论关闭相信大家在开发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是不是很好用呀,希望这样的一个小小的分享能对大家有所帮助咯
如需转载引用请注明出处:http://blog.csdn.net/jiahui524
相关文章推荐
- http学习笔记
- 【网络基本功系列三】细说VLAN与Trunk
- Android Volley完全解析(二),使用Volley加载网络图片
- Https环境下WS接口两次连续调用出错
- 厦门巨游网络科技有限公司(HOTPOWER)承接游戏UI外包
- HttpClient4.5.1简单封装
- iOS网络编程之六——数据缓存类NSURLCache使用解析
- iOS_SN_Socket网络编程(一)
- iOS 网络解析
- iOS_SN_基于AFNetworking3.0网络封装
- TCP/IP详解学习笔记
- linux(ubuntu)设置以太网络
- 几种TCP连接中出现RST的情况
- http和https的区别
- “一键”知道自己的IP地址和网络供应商
- 网络---大文件的下载(NSURLConnection)
- 网络---大文件的下载(NSURLSession)
- A/V data用TCP来接收(rtp overrtsp),可以收到数据,但解析不对。
- TCP三次握手/四次握手
- 【深度学习介绍系列之二】——深度强化学习:卷积神经网络