BaseActivity
BaseActivity是一款適配布局的框架模塊,能夠提供沉浸式狀態(tài)欄查近,可調(diào)用黑色狀態(tài)欄模式,判斷了小米MIUI和flyme系統(tǒng)獨立的黑色狀態(tài)欄樣式可獨立調(diào)用挤忙,并提供了一些簡單的吐司和生成日志的方法的基礎(chǔ)Activity類霜威,可以直接將您項目中的Activity繼成BaseActivity使用。
如何更加優(yōu)雅的編寫代碼是BaseActivity存在的主要目的册烈,也是本代碼誕生的原因戈泼。
本文GitHub地址:https://github.com/kongzue/BaseActivity
歡迎Fork&Star
6.0更新說明:
刪除冗余代碼;
將BaseActivity繼承改為AppCompatActivity赏僧,方便權(quán)限回調(diào)大猛;
取消了initStyle();方法,改為更切合原生的沉浸式適配方式淀零;
取消了sys_statusBar布局適配的必要性挽绩,采用更簡單的方式適配沉浸式;
整理代碼邏輯窑滞;
約束
-
6.0版本的BaseActivity為了更快更有效的完成Activity創(chuàng)建琼牧,因此特定的編寫了一些自動化執(zhí)行方法,如果開發(fā)者遵循我們的編寫流程和方案哀卫,可以快速完成各Android系統(tǒng)UI布局的適配操作以界面的沉浸式巨坊。
一般Activity需要完成的事項主要由以下及部分組成:
加載布局、處理適配問題此改、加載數(shù)據(jù)趾撵、控件組件綁定事件
對應(yīng)的,我們在BaseActivity中提供了相對應(yīng)的方法initViews()共啃、initDatas()占调、setEvent(),用戶繼承對應(yīng)的方法并重寫其代碼移剪,BaseActivity會自動依次執(zhí)行并對適配相關(guān)的事情進行處理究珊。
為方便適配沉浸式,請在Activity的layout布局的內(nèi)容部分使用 android:fitsSystemWindows="true" 加以約束纵苛,此時內(nèi)容布局會自動縮小為頂部狀態(tài)欄及底部導航欄以內(nèi)的位置:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/box_body"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.kongzue.baseframe.MainActivity">
<!--背景部分-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="horizontal">
<!--內(nèi)容部分-->
</LinearLayout>
</LinearLayout>
開始
現(xiàn)在剿涮,請跟著我的慢動作(劃掉),一起來加速編寫一個Activity攻人!
首先取试,你需要創(chuàng)建一個默認的Activity如下:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
接下來:
先刪除繼承的AppCompatActivity,轉(zhuǎn)而繼承我們的BaseActivity怀吻,
在onCreate方法中使用super.onCreate(Bundle,layoutRes)瞬浓,
重寫三個方法initViews()、initDatas()蓬坡、setEvent()猿棉,此處重寫三個方法的原因是這些方法會依次自動被執(zhí)行磅叛,我們保留了super.onCreate(Bundle)的原始方法,并提供了新的兩個參數(shù)的onCreate方法铺根,若使用此方法加載布局宪躯,則會直接依次執(zhí)行initViews()、initDatas()和setEvent()位迂。
默認會對狀態(tài)欄進行的是白色主題的適配访雪,如有黑色主題的需要,請單獨執(zhí)行setTranslucentStatus(true, true)方法即可掂林。
點擊Android Studio的Code -> Override Methods... 菜單臣缀,重寫上述方法,并設(shè)置super.onCreate(savedInstanceState,R.layout.activity_main)加載布局:
public class MainActivity extends BaseActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState,R.layout.activity_main);
}
@Override
public void initViews() {
//此處加載組件
}
@Override
public void initDatas() {
//此處編寫初始化代碼
}
@Override
public void setEvent() {
//此處為組件綁定事件
}
}
BaceActivity6.0版本旨在幫助開發(fā)者明晰代碼結(jié)構(gòu)與布局泻帮,輕松完成適配工作精置,同樣的,也提供了更為完善的小工具锣杂,例如簡易吐司脂倦、簡易Log日志、控制鍵盤開啟關(guān)閉元莫、dpi與px雙向計算轉(zhuǎn)換赖阻、Android6.0以上權(quán)限申請、屬性動畫踱蠢。
小工具調(diào)用方法說明
//簡易吐司:
toast(Obj);
//簡易Log打印日志:
log(Obj);
//軟鍵盤開關(guān):
setIMMStatus(boolean show, EditText editText);
//dip與像素px轉(zhuǎn)換:
dip2px(Context context, float dpValue);
//像素px與dip轉(zhuǎn)換:
dip2px(Context context, float dpValue);
//申請權(quán)限(0x0001是返回值火欧,需要的可以重寫onRequestPermissionsResult做回調(diào)處理):
requestPermission(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION
, Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN}, 0x0001);
//屬性動畫:
moveAnimation(Object obj, String perference, float aimValue, long time, long delay);
4.0版本說明:http://www.reibang.com/p/3905683c5df1
開源協(xié)議
Copyright Kongzue BaseActivity
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.