Android应用定制属于你的BaseActivity

本文介绍如何创建一个BaseActivity,封装通用代码,简化Android应用开发过程。通过继承该BaseActivity,开发者可以轻松复用标题、按钮等组件,并自定义内容布局,提高开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

}
});

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值