這可能是性能最優(yōu)、使用最簡單,支持自定義的標題欄
Github傳送地址,歡迎Star,Pull及issue
已投入公司項目多時,沒有任何毛病,點擊此處下載Demo
想了解實現(xiàn)原理的可以參考文章:純手工打造一個通用的標題欄TitleBar

集成步驟
dependencies {
implementation 'com.hjq:titlebar:5.0'
}
屬性大全(劃重點,要考)
<declare-styleable name="TitleBar" >
<!-- 整體樣式 -->
<attr name="barStyle">
<enum name="light" value="0x10" />
<enum name="night" value="0x20" />
<enum name="transparent" value="0x30" />
</attr>
<!-- 中間 -->
<attr name="title" format="string" />
<attr name="titleColor" format="color" />
<attr name="titleSize" format="dimension" />
<!-- 左邊 -->
<attr name="leftTitle" format="string"/>
<attr name="leftIcon" format="reference" /><!-- leftIcon 優(yōu)先于 backButton -->
<attr name="backButton" format="boolean" /><!-- 返回按鈕(默認開) -->
<attr name="leftColor" format="color" />
<attr name="leftSize" format="dimension" />
<attr name="leftBackground" format="reference|color" />
<!-- 右邊 -->
<attr name="rightTitle" format="string" />
<attr name="rightIcon" format="reference" />
<attr name="rightColor" format="color" />
<attr name="rightSize" format="dimension" />
<attr name="rightBackground" format="reference|color" />
<!-- 分割線 -->
<attr name="lineVisible" format="boolean" />
<attr name="lineColor" format="reference|color" />
<attr name="lineSize" format="dimension" />
</declare-styleable>
XML示例
<com.hjq.bar.TitleBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="默認的標題欄" />
設(shè)置監(jiān)聽事件
mTitleBar.setOnTitleBarListener(new OnTitleBarListener() {
@Override
public void onLeftClick(View v) {
ToastUtils.show("左項View被點擊");
}
@Override
public void onTitleClick(View v) {
ToastUtils.show("中間View被點擊");
}
@Override
public void onRightClick(View v) {
ToastUtils.show("右項View被點擊");
}
});
統(tǒng)一TitleBar樣式
如果對TitleBar的默認樣式不滿意,可以在Application初始化樣式,具體可參考TitleBarLightStyle的實現(xiàn)
public class XXApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
TitleBar.initStyle(new ITitleBarStyle());
}
}
混淆規(guī)則
-keep class com.hjq.bar.** {*;}
框架亮點
性能最佳:不使用LayoutInflater,而使用代碼創(chuàng)建View的形式
體驗最優(yōu):TitleBar默認樣式效果已經(jīng)非常好,可下載Demo測試
支持操控子控件:可以在代碼中獲取TitleBar的子控件進行調(diào)用相關(guān)的API
支持自定義布局:可將TitleBar當(dāng)做FrameLayout使用,直接在XML中自定義布局
兼容沉浸式狀態(tài)欄:兼容Github的沉浸式狀態(tài)欄框架,達到完全沉浸的效果
框架兼容性良好:本框架不依賴任何第三方庫,支持兼容所有的安卓版本
支持全局配置樣式:可以在Application中初始化TitleBar樣式,達到一勞永逸的效果