編輯:關於 Android 程式設計
先做前置準備。Android Studio 直接掛上相容庫:
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
Eclipse 專案則把對應的類別檔匯入即可。
<style name="ActionBarTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/ActionBarStyle</item>
</style>
<style name="ActionBarStyle" parent="android:Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">@color/actionbar_bg</item>
</style>
@TargetApi(19)
private void setTranslucentStatus(boolean on) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
}
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(R.color.statusbar_bg);
}
android:fitsSystemWindows="true"
少了這一行,App 的內容會直接頂進狀態列底下被遮住,這是新手最常踩的坑。
<style name="FullBleedTheme" parent="android:Theme.Holo.Light.NoActionBar.TranslucentDecor"> </style>
若使用 Toolbar,主題則需對應改成 AppCompat 系列:
<style name="AppThemeToolbar" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#2196F3</item>
<item name="colorPrimaryDark">#2196F3</item>
<item name="colorAccent">#E91E63</item>
</style>
mTintManager = new SystemBarTintManager(this); mTintManager.setStatusBarTintEnabled(true); mTintManager.setNavigationBarTintEnabled(true); mTintManager.setStatusBarTintResource(R.color.statusbar_bg);
android:fitsSystemWindows="true" android:clipToPadding="false"
這裡多出的 clipToPadding="false" 很關鍵。當列表類型的 View 設定了 padding 以避免內容被狀態列遮住時,若同時開著 clipToPadding,捲動時項目就會在 padding 區域被硬切成兩半;關掉之後,內容可以自然地滑到狀態列下方,視覺上就完整了。
兩種 Case 的核心流程其實一致:主題決定要不要 ActionBar 與是否透明裝飾,程式碼負責打開透明旗標並指定沉浸色,版面屬性則確保內容不會被系統 UI 蓋住。
Android 平台下透過 JNI 調用第三方 so 庫的實踐
在研究了幾天 JNI 之後,我在自己生成的 so 庫中調用第三方 so 庫時遇到了問題,解決之後特意整理並記錄一下。 首先說明一下網上關於調用第三方 so 庫的兩種常見說
Activity 生命週期詳解:七個回呼、四種狀態與實際驗證
用實驗把生命週期跑出來 研究 Activity 生命週期,最直接的方式就是把每個回呼都加上日誌,然後逐一操作、觀察 LogCat。測試用的程式碼相當簡單: package
Android BLE 開發:手機搜尋 iBeacon 基站
先前談過 Android 手機與 BLE 終端之間的通訊,而最常見的 BLE 終端應該是蘋果公司倡導的 iBeacon 基站。iBeacon 技術基於 BLE,其特點是透
Android 多擊手勢實作:從雙擊到 N 連擊的兩種寫法
最近稍微研究了一下多擊事件的處理,在這裡做個簡要整理,供大家指教。 方法一:比對時間差 最直覺的作法是在 ACTION_DOWN 時記錄時間戳,若與上次的間隔小於閾值(例