2018-04-22

【Android Studio】Snackbar -- 比 Toast 更多功能的 跳出式訊息

參考資料 ----
Building and Displaying a Pop-Up Message


Snackbar 是自 API22 新加入的功能,類似 Toast,也是跳出式的訊息視窗 (是不是要取代 Toast? 官方沒有明確表示) 。


SnackbarCoordinatorLayout 內顯示時,還能有額外的特殊功能:

1. 使用者能以 左/右 滑動的方式移除 Snackbar

2. 如果在 Layout 內有諸如 FloatingActionButton 的元件時,當跳出 Snackbar 顯示,Snackbar 會將 FloatingActionButton 往上推;等 Snackbar 退出畫面,FloatingActionButton 會再回到原位。


如果您的畫面是 FramLayout,您甚至可以直接以 CoordinatorLayout 取代,就能充分運用 Snackbar 的功能。


先看傳統的程式使用 Snackbar 的方式



 build.gradle(Module: app)
  1.  
  2. ...
  3. ...
  4.  
  5. dependencies {
  6. implementation fileTree(dir: 'libs', include: ['*.jar'])
  7. implementation 'com.android.support:appcompat-v7:27.1.1'
  8. implementation 'com.android.support:design:27.1.1' // 注意:要加上這行
  9. testImplementation 'junit:junit:4.12'
  10. androidTestImplementation 'com.android.support.test:runner:1.0.1'
  11. androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
  12. }
  13.  


activity_main.xml(採用 RelativeLayout)
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <RelativeLayout
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. xmlns:app="http://schemas.android.com/apk/res-auto"
  6. xmlns:tools="http://schemas.android.com/tools"
  7. android:id="@+id/mv" 注意:Layout 要有 id
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. tools:context=".MainActivity">
  11.  
  12. <Button
  13. android:id="@+id/button"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_alignParentStart="true"
  17. android:layout_alignParentLeft="true"
  18. android:layout_alignParentTop="true"
  19. android:layout_marginStart="16dp"
  20. android:layout_marginLeft="16dp"
  21. android:layout_marginTop="16dp"
  22. android:text="Button"
  23. tools:layout_editor_absoluteX="16dp"
  24. tools:layout_editor_absoluteY="16dp"
  25. android:onClick="ShowSnackBar"/>
  26.  
  27. </RelativeLayout>
  28.  


MainActivity.java
  1.  
  2. ...
  3. ...
  4.  
  5. public void ShowSnackBar(View v)
  6. {
  7. Snackbar.make(findViewById(R.id.mv), "顯示 Snackbar", Snackbar.LENGTH_SHORT)
  8. .show();
  9. }
  10.  





稍微修改 activity_mail.xml
  1.  
  2. <!-- 在 RelativeLayout 外層包上 CoordinatorLayout, 並微調 -->
  3. <?xml version="1.0" encoding="utf-8"?>
  4. <android.support.design.widget.CoordinatorLayout
  5. android:id="@+id/mv"
  6. xmlns:android="http://schemas.android.com/apk/res/android"
  7. xmlns:app="http://schemas.android.com/apk/res-auto"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent">
  10.  
  11. <RelativeLayout
  12. xmlns:tools="http://schemas.android.com/tools"
  13. android:layout_width="match_parent"
  14. android:layout_height="match_parent"
  15. tools:context=".MainActivity">
  16.  
  17. <Button
  18. android:id="@+id/button"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_alignParentStart="true"
  22. android:layout_alignParentLeft="true"
  23. android:layout_alignParentTop="true"
  24. android:layout_marginStart="16dp"
  25. android:layout_marginLeft="16dp"
  26. android:layout_marginTop="16dp"
  27. android:text="Button"
  28. tools:layout_editor_absoluteX="16dp"
  29. tools:layout_editor_absoluteY="16dp"
  30. android:onClick="ShowSnackBar"/>
  31.  
  32. </RelativeLayout>
  33.  
  34. </android.support.design.widget.CoordinatorLayout>
  35.  



Snackbar 顯示時,往右滑移除。



Snackbar 還可以加入 文字按鈕,並在使用者點擊時執行指定的動作。
  1.  
  2. public void ShowSnackBar(View v)
  3. {
  4. Snackbar.make(findViewById(R.id.mv), "顯示 Snackbar", Snackbar.LENGTH_LONG)
  5. .setAction("點擊執行", new View.OnClickListener()
  6. {
  7. @Override
  8. public void onClick(View v)
  9. {
  10. // 點擊訊息時要執行的動作
  11. Toast.makeText(MainActivity.this, "點擊了 Snackbar", Toast.LENGTH_SHORT).show();
  12. }
  13. })
  14. .setActionTextColor(Color.YELLOW) // 還可以指定 文字顏色
  15. .show();
  16. }
  17.