本文GitHub地址:https://github.com/kongzue/BaseActivity
歡迎Fork&Star
BaseActivity
BaseActivity是一款適配布局的框架模塊塞帐,能夠提供沉浸式狀態(tài)欄率触,可調(diào)用黑色狀態(tài)欄模式签钩,判斷了小米MIUI和flyme系統(tǒng)獨(dú)立的黑色狀態(tài)欄樣式可獨(dú)立調(diào)用,并提供了一些簡單的吐司和生成日志的方法的基礎(chǔ)Activity類,可以直接將您項目中的Activity繼成BaseActivity使用弓颈。
如何更加優(yōu)雅的編寫代碼是BaseActivity存在的主要目的哆致,也是本代碼誕生的原因。
5.0更新說明:
對于UI適配更加優(yōu)雅摄咆。
??約束
- 5.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)的事情進(jìn)行處理。 - 直接導(dǎo)入BaseActivity可能提示出現(xiàn)錯誤,有幾個布局找不到:R.id.sys_statusBar以及R.id.box_body可能缺失勉盅,這兩個布局也是約定布局佑颇,您需要在Activity對應(yīng)的Layout中加入他們,我們的標(biāo)準(zhǔn)Activity的Layout布局文件Demo如下:
<?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"
android:focusable="true"
android:focusableInTouchMode="true"
tools:context="com.kongzue.baseframe.MainActivity">
<LinearLayout
android:id="@+id/sys_statusBar"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="vertical"
android:background="#303F9F"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#3F51B5" >
<TextView
android:id="@+id/txt_title"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_weight="1"
android:layout_marginLeft="20dp"
android:layout_centerHorizontal="true"
android:gravity="center_vertical"
android:textSize="20sp"
android:textColor="#ffffff"
android:text="測試標(biāo)題"/>
</LinearLayout>
<!--其他布局-->
</LinearLayout>
可以看到菇篡,設(shè)置了主父布局的ID為box_body漩符,BaseActivity會對其高度進(jìn)行修改,修改方式為(屏幕高度-狀態(tài)欄高度-底部導(dǎo)航欄高度)此處會依據(jù)不同手機(jī)——例如魅族驱还、小米等廠商不同的情況進(jìn)行調(diào)整嗜暴。
我們還在主布局下加入了一個新的布局sys_statusBar,該布局是狀態(tài)欄占位布局议蟆,該布局將會占用系統(tǒng)狀態(tài)欄的位置闷沥,將其他布局向下推移一段距離,避免Activity界面內(nèi)容與狀態(tài)欄透明后的位置重疊咐容。該布局樣式可修改舆逃。
開始
現(xiàn)在,請跟著我的慢動作(劃掉)戳粒,一起來加速編寫一個Activity路狮!
首先,你需要創(chuàng)建一個默認(rèn)的Activity如下:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
注意蔚约,重點(diǎn)開始了奄妨!
- 先刪除繼承的AppCompatActivity,轉(zhuǎn)而繼承我們的BaseActivity苹祟,
- 在onCreate方法中使用super.onCreate(Bundle,layoutRes)砸抛,
- 重寫三個方法initViews()、initDatas()树枫、setEvent()直焙,此處重寫三個方法的原因是這些方法會依次自動被執(zhí)行,我們保留了super.onCreate(Bundle)的原始方法砂轻,并提供了新的兩個參數(shù)的onCreate方法奔誓,若使用此方法加載布局,則會直接依次執(zhí)行initViews()搔涝、initStyle()厨喂、initDatas()和setEvent()。
注意体谒,沉浸式方法initStyle()該方法不需要重寫,會自動執(zhí)行臼婆。默認(rèn)會對狀態(tài)欄進(jìn)行的是白色主題的適配抒痒,如有黑色主題的需要,請單獨(dú)執(zhí)行setTranslucentStatus(true, true)方法即可颁褂。
點(diǎn)擊Android Studio的Code -> Override Methods... 菜單故响,重寫上述方法傀广,并設(shè)置super.onCreate(savedInstanceState,R.layout.activity_main)加載布局:
public class MainActivity extends BaseActivity {
private LinearLayout boxBody;
private LinearLayout sysStatusBar;
private TextView txtTitle;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState,R.layout.activity_main);
}
@Override
public void initViews() {
//此處加載組件
boxBody = (LinearLayout) findViewById(R.id.box_body);
sysStatusBar = (LinearLayout) findViewById(R.id.sys_statusBar);
txtTitle = (TextView) findViewById(R.id.txt_title);
}
@Override
public void initDatas() {
//此處編寫初始化代碼
}
@Override
public void setEvent() {
//此處為組件綁定事件
}
}
其他一些建議
- 約定布局:占位布局sys_statusBar是為了將Activity中的主體部分(包含標(biāo)題欄和內(nèi)容)向下擠壓以免其位置與系統(tǒng)狀態(tài)欄存在的位置重疊,且在Android4.4以下版本(不支持沉浸式的版本)下sys_statusBar布局彩届,會隱藏伪冰,以系統(tǒng)黑色狀態(tài)欄為呈現(xiàn)方式。
- 建議在內(nèi)容部分外嵌套一個ScrollView樟蠕,并在內(nèi)容部分的容器View中使用:
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
在鍵盤彈起時布局會自動上移且內(nèi)容部分的輸入框可以上彈到屏幕視野內(nèi)贮聂,從而解決沉浸式鍵盤無法上移的問題。
BaceActivity5.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.