XBaseAndroid
項(xiàng)目地址:https://github.com/fanhua1994/XBaseAndroid
演示地址:https://github.com/fanhua1994/XBaseAndroidDemo
如果您需要使用最新版可以去github查看盾剩。該文章描述的是1.0.9.4版本八酒。
使用AndroidStudio 3.0創(chuàng)建一個(gè)新的安卓項(xiàng)目
如果您不知道如何創(chuàng)建梅尤,請(qǐng)猛戳此處。
配置倉(cāng)庫(kù)引用
點(diǎn)擊build.gradle(Project),添加以下maven倉(cāng)庫(kù)骑冗。maven { url 'https://jitpack.io' }
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}
點(diǎn)擊build.gradle(Module).在dependencies 下添加以下引用秉宿。
dependencies {
compile 'com.github.fanhua1994:XBaseAndroid:1.0.9.4'
}
1.0.7.1以后版本必須引入以下包
implementation 'com.google.code.gson:gson:2.6.2'
implementation 'com.lzy.net:okgo:3.0.4'
implementation 'org.greenrobot:eventbus:3.1.1'
implementation 'com.j256.ormlite:ormlite-core:4.48'
implementation 'com.j256.ormlite:ormlite-android:4.48'
初始化框架
打開AndroidManifest.xml
在application節(jié)點(diǎn)下做出如下整改
android:name="com.hengyi.baseandroidcore.base.XBaseApplication"
android:theme="@style/NoActionBarTheme"
使用視圖注解
加入以下引用
compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
編寫B(tài)aseActivity
package com.hengyi.xbaseandroiddemo;
import android.os.Bundle;
import com.hengyi.baseandroidcore.base.XBaseActivity;
import butterknife.ButterKnife;
import butterknife.Unbinder;
//也可以繼承XBasePermissionActivity 申請(qǐng)權(quán)限更方便
/**
* requestPermisstion();//發(fā)起權(quán)限請(qǐng)求
*/
public abstract class BaseActivity extends XBaseActivity {
private Unbinder unbind = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
unbind = ButterKnife.bind(this);
}
@Override
public int setBaseContentView() {
return setContentView();
}
public abstract int setContentView();
@Override
protected void onDestroy() {
super.onDestroy();
unbind.unbind();
}
}
讓MainActivity繼承BaseActivity
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LogUtils.d("安裝日志","XBaseAndroid初始化成功");
}
@Override
public int setContentView() {
return R.layout.activity_main;
}
}
啟動(dòng)App
如果現(xiàn)實(shí)“XBaseAndroid初始化成功”,并且觀察Logcat氯迂,有日志打印如下践叠。
12-28 14:20:46.277 11538-11538/com.hengyi.xbaseandroiddemo D/安裝日志: ╔═══════════════════════════════════════════
12-28 14:20:46.277 11538-11538/com.hengyi.xbaseandroiddemo D/安裝日志: ║ main, onCreate(MainActivity.java:11)
12-28 14:20:46.277 11538-11538/com.hengyi.xbaseandroiddemo D/安裝日志: ╟───────────────────────────────────────────
12-28 14:20:46.277 11538-11538/com.hengyi.xbaseandroiddemo D/安裝日志: ║ XBaseAndroid初始化成功
12-28 14:20:46.277 11538-11538/com.hengyi.xbaseandroiddemo D/安裝日志: ╚═══════════════════════════════════════════
若您的運(yùn)行情況出現(xiàn)問題或不一致,請(qǐng)下載demo比對(duì)