LoginSignup
15
18

More than 5 years have passed since last update.

すぐできるステータスバーの透過対応

Posted at

API16以上で確認しました。

dimen要素の追加

values/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="toolbar_height">56dip</dimen>
    <dimen name="status_bar_height">24dp</dimen>
    <dimen name="toolbar_padding">0dp</dimen>
</resources>
values-v21/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="toolbar_height">80dip</dimen>
    <dimen name="status_bar_height">0dp</dimen>
    <dimen name="toolbar_padding">24dp</dimen>
</resources>

styleの指定

values/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="YourTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="colorPrimary">@color/primary</item>
        <!-- 好きな色で透過 -->
        <item name="colorPrimaryDark">@color/black_alpha_40</item>
    </style>
</resources>
values-v21/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="YourTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="windowActionModeOverlay">true</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>
</resources>

アプリ内の各Toolbarのレイアウトを修正

toolbar_common.xml
<android.support.v7.widget.Toolbar
                android:id="@+id/some_toolbar"
                android:layout_width="match_parent"
                android:layout_height="@dimen/toolbar_height"
                android:background="@android:color/white"
                android:paddingTop="@dimen/toolbar_padding" />

その他

  • 独自Viewを使っている画面でToolbarの高さを使った処理を行っている場合は対応が別途必要
  • Drawerを使っている場合はpaddingを設定してコンテンツに被らないようにする

これだけです :joy_cat:

15
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
18