LoginSignup
32
17

More than 5 years have passed since last update.

Android で Snackbar の表示位置 & 幅を Material Design ガイドラインに則ってカスタマイズしてみた件

Posted at
1 / 30

Snackbars - Material Design

  • 画面下部で一時的に表示されるアプリケーションのプロセスに関する簡単なメッセージ

snackbars-container-do-1.png


推奨 :o:

  • コンポーネント上部での表示が理想
FloatingActionButton BottomAppBar
snackbars-layout-fab.png snackbars-layout-do-navigation.png

模範例 :thumbsup:

Screenshot_1526031261.png


駄目 :x:

  • コンポーネントの前面に被せるのは厳禁
FloatingActionButton BottomAppBar
snackbars-layout-fab-dont-1.png snackbars-layout-don-t-bottom-bar.png

疑惑 :police_car:

20180511_172225.jpg


アカンやつ :no_good:

  • 画面底の永続的要素を押し上げて、ひょっこりはん(例:BottomAppBar

snackbars-layout-don-t-push-bottom-bar-1.png


事案 :warning:

IMG_0008.PNG


:one: 任意の位置から

  • Snackbar を画面の底からではなく、指定の場所から表示させたい :pray:

方法

  1. レイアウト内で Snackbar を表示させたい地点に CoordinatorLayout を配置
  2. Snackbar.make メソッドの第1引数に、レイアウト内に設置した CoordinatorLayout を渡す

参考 :link:


サンプル

手法

  • Android Studio 内のテンプレート Bottom Navigation Activity を少し修正します :tools:

目標

Screenshot_1526083703.png


コード

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <android.support.design.widget.BottomNavigationView
        android:id="@id/bottomNavigationView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>

MainActivity.kt
import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        bottomNavigationView.setOnNavigationItemSelectedListener({ menuItem ->
            Snackbar.make(coordinatorLayout, menuItem.title, Snackbar.LENGTH_SHORT).show()
            true
        })
    }
}

実行結果

Screenshot_1525876113.png


:two: 任意の幅で


幅を "wrap_content" にした場合

  • CoordinatorLayoutandroid:layout_width="wrap_content" にすると、表示される Snackbar の幅がテキストの長さ分になります :straight_ruler:

コード

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <android.support.design.widget.BottomNavigationView
        android:id="@id/bottomNavigationView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>

実行結果

例 ① 例 ②
Screenshot_1525876227.png Screenshot_1525876231.png

サイズを指定


サンプル

参考 :link:


コード

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintWidth_percent="0.1" />

    <android.support.design.widget.BottomNavigationView
        android:id="@id/bottomNavigationView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>

実行結果

例 ① 例 ②
Screenshot_1525876506.png Screenshot_1525876512.png

:three: 任意の位置 & 幅で

左揃え 中央揃え
snackbars-layout-desktop-do.png snackbars-container-landscape-do-2.png

実例

  • Web 版 Google+ で「フォロー」ボタン押下後に表示される Snackbar

スクリーンショット.png


YouTube

  • Android タブレットでは右側(左側は高評価ボタンと重なり邪魔になるからと忖度 :thought_balloon:

Screenshot_1526177863.png


サンプル


コード

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintStart_toStartOf="parent" />

    <android.support.design.widget.BottomNavigationView
        android:id="@id/bottomNavigationView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>

実行結果

Screenshot_1525876646.png


:end:

32
17
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
32
17