Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 開源 UI 元件庫 BottomView:底部彈出面板實作

編輯:關於 Android 程式設計

分享一個相當實用又簡單的 Android 第三方 UI 元件:BottomView。小米的米 UI 也用到了類似方案。

它能做什麼

  • 在從底部彈出的 View 中自訂任意版面配置
  • 自訂是否點擊外部區域即可關閉
  • 自訂回呼事件
  • 自訂外圍背景是否透明
  • 自訂進出場動畫
  • 必要時可強制讓頂層 View 顯示

使用步驟

1. 加入函式庫

下載 BottomView.jar,放到 Android 專案的 libs 目錄下。

2. 設定 Theme

把以下兩個 Theme 複製到專案的 res/values/styles.xml:

<!-- 半透明背景 Theme -->
<style name="BottomViewTheme_Defalut">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowIsTranslucent">false</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@color/white</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:windowFullscreen">true</item>
</style>

<!-- 透明背景 Theme -->
<style name="BottomViewTheme_Transparent">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowIsTranslucent">false</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@color/white</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

如果提示 @color/white 找不到,表示專案的 res/values/colors.xml 裡還沒有這個顏色。補一行即可:

<color name="white">#ffffff</color>

3. 加上動畫(建議)

有進出場動畫,整體質感會好很多:

<style name="BottomToTopAnim" parent="android:Animation">
    <item name="@android:windowEnterAnimation">@anim/bottomview_anim_enter</item>
    <item name="@android:windowExitAnimation">@anim/bottomview_anim_exit</item>
</style>

res/anim/bottomview_anim_enter.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="100%p"
        android:toYDelta="0"
        android:duration="300" />
</set>

res/anim/bottomview_anim_exit.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="0"
        android:toYDelta="100%p"
        android:duration="300" />
</set>

整體目錄結構如下圖。

核心使用程式碼

BottomView bottomView = new BottomView(this, R.style.BottomViewTheme_Defalut,
                                       R.layout.bottom_view);
bottomView.setAnimation(R.style.BottomToTopAnim);  // 動畫,可選
bottomView.showBottomView(false);

需要取得這個 View 本身的話,呼叫 getView() 即可。

效果圖之一,內容完全可以自行發揮:

下載位置:

  • BottomView.jar:https://pan.baidu.com/s/1mg7eseG
  • Demo:https://pan.baidu.com/s/1hqkRM8s

原文出處:https://www.aplesson.com/?p=400

幾點實用提醒

這個元件的原理是把自訂版面塞進一個浮動的 Window,靠 Theme 控制背景、遮罩與動畫——理解這點之後,其實不用第三方庫也能自己實作:一個 Dialog 加上 window.setGravity(Gravity.BOTTOM)、透明背景與進出場動畫,效果基本相同。

若要在自己的專案裡用類似的底部面板,AndroidX 提供的 BottomSheetDialog 是更標準的選擇:支援拖曳手勢、多種展開狀態(收合/半開/展開)、內建遮罩與系統整合,也不需要另外拉第三方 jar。

另外原文中的 BottomViewTheme_Defalut 拼字有誤(應為 Default),複製時保持一致即可,但新專案建議直接改成正確拼法,免得之後維護時困惑。

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