
圖片來源網(wǎng)絡(luò),入侵必刪
在日常的Android開發(fā)當中,我們肯定實現(xiàn)應(yīng)用的頭部欄需求。在這篇博客當中,我分享一個我常用的TitleBar開源庫,希望能幫小伙伴們提高開發(fā)效率。
引入項目
Gradle 配置是在7.0以下,需要在項目級別的build.gradle文件中加入
allprojects {
repositories {
// JitPack 遠程倉庫:https://jitpack.io
maven { url 'https://jitpack.io' }
}
}
Gradle 配置是在7.0以上,需要在項目的settings.gradle文件中加入
dependencyResolutionManagement {
repositories {
// JitPack 遠程倉庫:https://jitpack.io
maven { url 'https://jitpack.io' }
}
}
最后在項目app模塊下的build.gradle文件中加入:
android {
// 支持 JDK 1.8
compileOptions {
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
// 標題欄框架:https://github.com/getActivity/TitleBar
implementation 'com.github.getActivity:TitleBar:9.5'
}
開源庫的特點
- 性能最佳:不使用 LayoutInflater,而使用代碼創(chuàng)建 View 的形式
- 體驗最優(yōu):TitleBar 默認樣式效果已經(jīng)非常好,可下載 Demo 測試
- 支持操控子控件:可以在代碼中獲取 TitleBar 的子控件進行調(diào)用相關(guān)的 API
- 兼容沉浸式狀態(tài)欄:兼容 Github 的沉浸式狀態(tài)欄框架,達到完全沉浸的效果
- 框架兼容性良好:本框架不依賴任何第三方庫,支持兼容所有的安卓版本
- 支持全局配置樣式:可以在 Application 中初始化 TitleBar 樣式,達到一勞永逸的效果
使用示例
XML的使用:
<com.hjq.bar.TitleBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:title="默認的標題欄" />
代碼中實現(xiàn)點擊事件監(jiān)聽:
titleBar.setOnTitleBarListener(new OnTitleBarListener() {
@Override
public void onLeftClick(TitleBar titleBar) {
ToastUtils.show("左項 View 被點擊");
}
@Override
public void onTitleClick(TitleBar titleBar) {
ToastUtils.show("中間 View 被點擊");
}
@Override
public void onRightClick(TitleBar titleBar) {
ToastUtils.show("右項 View 被點擊");
}
});
上面是簡單的使用方法,更多定制化的需求需要自己去探索一下。一般情況下能滿足我們大部分的需求。