在Android开发中作为四大组件之一的Activity的使用频率非常高,我们的应用展现给用户的每一个界面基本上都离不开Activity。在一个项目中会用到多个Activity用于展示不同内容,虽然展示的内用不同,但是通过观察界面和和代码就会发现有些代码在几个Activity基本是类似的,比如一些头布局和一些广播的注册等。这些代码我们就可以抽取出来封装到一个共同的父类中,具体的实现在之类中实现,这样就可以方便我们日后维护,也会提高我们的开发效率。例如像下面的这中界面的布局
BaseActivity
public abstract class BaseActivity extends Activity {
protected final String TAG = this.getClass().getSimpleName();
private LinearLayout contentLayout;
private TextView title;
private Button backButton;
private Button moreButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.setContentView(R.layout.activity_base);
contentLayout = findViewById(R.id.content_layout);
backButton = findViewById(R.id.back);
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onBack();
}
});
title = findViewById(R.id.title);
moreButton = findViewById(R.id.more);
moreButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onMore();
}
});
initView();
initData();
}
/**
* 更多
*/
protected void onMore() {
}
/**
* 返回键
*/
protected void onBack() {
}
@Override
public void setContentView(View view) {
contentLayout.addView(view);
}
@Override
public void setContentView(int layoutResID) {
View view = View.inflate(this, layoutResID, null);
setContentView(view);
}
/**
* 是否全屏显示
* @param isVisible
*/
public void showWindowsTitle(boolean isVisible) {
if (!isVisible) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
}
/**
* 设置标题
* @param text
*/
public void setTitle(CharSequence text) {
title.setText(text);
}
/**
* 设置标题
* @param resid
*/
public void setTitle(int resid) {
title.setText(resid);
}
/**
* 初始化控件
*/
public abstract void initView();
/**
* 初始化数据
*/
public abstract void initData();
/**
* 界面跳转
* @param packageContext
* @param cls
* @param extras
*/
public void startActivity(Context packageContext, Class<?> cls, Bundle extras) {
Intent intent = new Intent(packageContext, cls);
if (extras != null) {
intent.putExtras(extras);
}
startActivity(intent);
}
}
BaseActivity的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BaseActivity">
<RelativeLayout
android:id="@+id/header_layout"
android:layout_width="match_parent"
android:background="#2d72e9"
android:layout_height="40dp">
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="@string/string_back" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/string_title" />
<Button
android:id="@+id/more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="@string/string_more" />
</RelativeLayout>
<!--内容部分,需要加入子类的布局-->
<LinearLayout
android:id="@+id/content_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/header_layout"
android:orientation="vertical" />
它的一个子类
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void initView() {
}
@Override
public void initData() {
}
}
子类的布局文件
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
对BaseActivity 的封装它的优势是显而易见的。
1.提取公共代,码便于维护
2.减少编写代码,提高开发效率
这是我对BaseActivity封装的初步理解,后期有新的发现总结还会持续更新
https://github.com/songdaren123/BaseActivity