Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 資訊

BGATitlebar:一個可定製的 iOS 風格標題欄

編輯:Android 資訊

工作以來,公司 UI 設計師給的 Android 效果圖一直是 iOS 風格的 Titlebar。新項目還要沿用同一套,實在不想重複造輪子,於是趁著這次練習「仿新浪微博 Android 客戶端」,順手抽出一個通用的 iOS 風格 Titlebar。

不過身為 Android 開發者,平時練習還是應該緊跟 Google 的節奏,說不定哪天設計師們就換成一套 MD 風格的效果圖了。

Demo 中演示了各種標題形態,以及新浪微博首頁選擇微博分類的案例。

效果圖

基本使用

1. 添加 Gradle 依賴

dependencies {
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'cn.bingoogolapple:bga-titlebar:latestVersion@aar'
}

2. 在布局文件中添加 BGATitlebar

<cn.bingoogolapple.titlebar.BGATitlebar
    android:id="@+id/titlebar"
    app:bgatitlebar_leftDrawable="@drawable/selector_nav_friendsearch"
    app:bgatitlebar_rightDrawable="@drawable/selector_nav_pop"
    app:bgatitlebar_titleDrawable="@drawable/selector_nav_arrow_orange"
    app:bgatitlebar_titleDrawablePadding="3dp"
    app:bgatitlebar_titleText="bingoogolapple" />

3. 在 Activity 或 Fragment 中配置 BGATitlebar

mTitlebar = (BGATitlebar) findViewById(R.id.titlebar);
mTitlebar.setDelegate(new BGATitlebar.BGATitlebarDelegate() {
    @Override
    public void onClickLeftCtv() {
        // 可選,根據實際業務重寫該方法
    }

    @Override
    public void onClickTitleCtv() {
        // 可選,根據實際業務重寫該方法
    }

    @Override
    public void onClickRightCtv() {
        // 可選,根據實際業務重寫該方法
    }
});

自定義屬性說明

建議把下面這五項定義在項目的 styles.xml 裡

  • bgatitlebar_leftAndRightTextColor:左右按鈕文字顏色
  • bgatitlebar_titleTextColor:中間標題文字顏色
  • bgatitlebar_leftAndRightTextSize:左右按鈕文字大小
  • bgatitlebar_titleTextSize:中間標題文字大小
  • bgatitlebar_leftAndRightPadding:左右按鈕在水平方向上的 padding

下面這幾項按每個頁面的業務寫在 layout 中(帶返回按鈕的 titlebar 也可以單獨抽一個 style)

  • bgatitlebar_leftText:左邊按鈕的文字
  • bgatitlebar_rightText:右邊按鈕的文字
  • bgatitlebar_titleText:中間標題文字
  • bgatitlebar_leftDrawable:左邊按鈕圖標
  • bgatitlebar_rightDrawable:右邊按鈕圖標
  • bgatitlebar_titleDrawable:中間標題圖標
  • bgatitlebar_titleDrawablePadding:中間按鈕文本與圖標之間的間距(同時設置 titleText 與 titleDrawable 時使用,例如新浪微博首頁選擇微博分類)
  • bgatitlebar_leftDrawablePadding:左邊按鈕文本與圖標之間的間距(同時設置 leftText 與 leftDrawable 時使用)
  • bgatitlebar_rightDrawablePadding:右邊按鈕文本與圖標之間的間距(同時設置 rightText 與 rightDrawable 時使用)

下面三項通常不用動,維持默認即可。只有在標題特別長而左右文字很短、或左右文字特別長而標題很短的極端情況下才需要單獨配置。

  • bgatitlebar_leftMaxWidth:左邊按鈕的最大寬度
  • bgatitlebar_rightMaxWidth:右邊按鈕的最大寬度
  • bgatitlebar_titleMaxWidth:中間標題的最大寬度

下面三項同樣通常不用動,使用默認值就好。

  • bgatitlebar_isTitleTextBold:標題文字是否為粗體,默認 true
  • bgatitlebar_isLeftTextBold:左邊文字是否為粗體,默認 false
  • bgatitlebar_isRightTextBold:右邊文字是否為粗體,默認 false

代碼是最好的老師,更多細節請直接看 Demo。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved