android - 在顶部(工具栏下方)显示一个 SnackBar

标签 android material-design android-snackbar

我试图在我的 MainActivity 中的 ToolBar 下方显示一个 SnackBar

我试过这段代码:

 mToolbar = (Toolbar) findViewById(R.id.toolbar);
 Snackbar snack = Snackbar.make(findViewById(android.R.id.content), "Had a snack at Snackbar", Snackbar.LENGTH_LONG);
    View view = snack.getView();
    FrameLayout.LayoutParams params =(FrameLayout.LayoutParams)view.getLayoutParams();
    params.gravity = Gravity.TOP;
    view.setLayoutParams(params);
    snack.show();

但是 SnackBar 显示在顶部,但不在 Toolbar 下方。

我想要类似于 Hangout 应用程序的东西,例如:

Screen shot

主要 Activity :

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/Coordinator"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity">

   <LinearLayout
       android:orientation="vertical"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent">

       <android.support.v7.widget.Toolbar

           android:id="@+id/toolbar"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:minHeight="?attr/actionBarSize"
           android:background="?attr/colorPrimary"
           android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

           <ImageView
               android:layout_width="75dp"
               android:layout_height="28dp"
               android:src="@drawable/athena"/>
      </android.support.v7.widget.Toolbar>

      <ProgressBar
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:id="@+id/progressBar5"
           android:layout_marginTop="10dp"
           android:layout_gravity="center_horizontal" />

      <android.support.v4.widget.SwipeRefreshLayout
           android:id="@+id/swipeRefreshLayout"
           android:layout_width="match_parent"
           android:layout_height="match_parent">

           <android.support.v7.widget.RecyclerView
               android:id="@+id/my_recycler_view"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:scrollbars="vertical" />
       </android.support.v4.widget.SwipeRefreshLayout>
   </LinearLayout>

    <com.melnykov.fab.FloatingActionButton
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="16dp"
        fab:fab_colorNormal="@color/colorPrimary"
        android:src="@drawable/ic_action_name9" />

</android.support.design.widget.CoordinatorLayout>

我正在使用 TSnackbar,SnackBar 显示在工具栏上

TSnackbar.make(findViewById(android.R.id.content),"Hello from TSnackBar.",TSnackbar.LENGTH_LONG).show();

最佳答案

在 snackbar 顶部设置边距

Snackbar snack = Snackbar.make(findViewById(android.R.id.content), "Had a snack at Snackbar", Snackbar.LENGTH_LONG);
View view = snack.getView();
FrameLayout.LayoutParams params =(FrameLayout.LayoutParams)view.getLayoutParams();
params.gravity =  Gravity.CENTER_HORIZONTAL | Gravity.TOP;

// calculate actionbar height
TypedValue tv = new TypedValue();
int actionBarHeight=0;
if (getTheme().resolveAttribute(R.attr.actionBarSize, tv, true))
{
    actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}

// set margin 
params.setMargins(0, actionBarHeight, 0, 0);

view.setLayoutParams(params);
snack.show();

关于android - 在顶部(工具栏下方)显示一个 SnackBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33451444/

相关文章:

android - VectorDrawableCompat 的用途是什么?

android - Retrofit2:使用拦截器在查询参数中添加访问 token

android - 如何为按钮提供阴影

Android资源链接失败错误: resource style/CardView error: failed linking references

android - 更新支持库后 RecyclerView 无法工作

java - 从非 UI 线程调用 Snackbar.make() 是如何工作的?

java - 不关闭游标或数据库对象?

Android Wi-Fi Direct 读取 rssi 信号强度

android - SnackBar 导致 ScrollView 崩溃并出现非法状态异常

android - Snackbar 不会在滑动时关闭