Android 封装BasseActivity

在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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值