手记

Android标题栏(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 优先于 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示例

点我查看完整的Demo示例

<com.hjq.bar.TitleBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:title="默认的标题栏" />

设置监听事件

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被点击");
    }
});

统一TitleBar样式

如果对TitleBar的默认样式不满意,可以在Application初始化样式,具体可参考TitleBarLightStyle的实现

public class XXApplication extends Application {    @Override
    public void onCreate() {        super.onCreate();
        TitleBar.initStyle(new ITitleBarStyle());
    }
}

混淆规则

-keep class com.hjq.bar.** {*;}

框架亮点

  • 性能最佳:不使用LayoutInflater,而使用代码创建View的形式

  • 体验最优:TitleBar默认样式效果已经非常好,可下载Demo测试

  • 支持操控子控件:可以在代码中获取TitleBar的子控件进行调用相关的API

  • 支持自定义布局:可将TitleBar当做FrameLayout使用,直接在XML中自定义布局

  • 兼容沉浸式状态栏:兼容Github的沉浸式状态栏框架,达到完全沉浸的效果

  • 框架兼容性良好:本框架不依赖任何第三方库,支持兼容所有的安卓版本

  • 支持全局配置样式:可以在Application中初始化TitleBar样式,达到一劳永逸的效果



作者:Android轮子哥
链接:https://www.jianshu.com/p/617be02dc265


0人推荐
随时随地看视频
慕课网APP