15

我试图SnackBarToolBar我的MainActivity,

我试过这段代码:

 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应用程序的东西,例如:

截屏

主要活动:

<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();
4

6 回答 6

2

在小吃店顶部设置边距

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();
于 2016-07-28T06:53:55.903 回答
0

我在下面给出了我在我的应用程序中使用的代码,该代码适用于各种设备

Complie  : compile 'com.androidadvance:topsnackbar:1.1.1'

定义视图:

 private CoordinatorLayout CSnakbarLayout;

CSnakbarLayout = (CoordinatorLayout) rootView.findViewById(R.id.snackbar_event_mesg);

XML:

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/snackbar_event_mesg"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true">

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

下面是您想要从顶部实现 Snackbar 的活动/片段中的代码:

/**
 * Method to display message for on going Event
 */
private void displaySnackbar() {
    int duration = 4000;
    final TSnackbar snackbar = TSnackbar.make(CSnakbarLayout, getString(R.string.on_going_event_mesg), TSnackbar.LENGTH_INDEFINITE);
    snackbar.setActionTextColor(Color.WHITE);
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            try {
                snackbar.dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }, duration);
    snackbar.setAction("Ok", new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            snackbar.dismiss();
        }
    });
    View snackbarView = snackbar.getView();
    ViewGroup.LayoutParams params = snackbarView.getLayoutParams();
    params.height = 300;
    snackbarView.setLayoutParams(params);
    snackbarView.setBackgroundColor(Color.BLACK);
    TextView textView = (TextView) snackbarView.findViewById(com.androidadvance.topsnackbar.R.id.snackbar_text);
    textView.setTextColor(Color.WHITE);
    textView.setMaxLines(5);
    snackbar.show();
}

谢谢 :)

于 2017-10-10T07:06:49.303 回答
0

感谢@Shijil 解决方案,我设法显示MinimalKSnack在工具栏下方。

MinimalKSnack minimalKSnack = new MinimalKSnack(MainActivity.this);
minimalKSnack.setMessage(getResources().getString(R.string.notification))
    .setStyle(MinimalKSnackStyle.STYLE_SUCCESS)
    .setBackgroundColor(R.color.metallic_blue)
    .setAnimation(Fade.In.getAnimation(), Fade.Out.getAnimation())
    .setDuration(2000); 

View view = minimalKSnack.getMinimalSnackView();

LinearLayout.LayoutParams params =(LinearLayout.LayoutParams)view.getLayoutParams();
params.gravity =  Gravity.CENTER_VERTICAL | Gravity.TOP;

TypedValue typedValue = new TypedValue();
int actionBarHeight =0;

if(getTheme().resolveAttribute(R.attr.actionBarSize,typedValue,true)){
    actionBarHeight = TypedValue.complexToDimensionPixelOffset(typedValue.data,getResources().getDisplayMetrics());
}

params.setMargins(0,actionBarHeight,0,0);
view.setLayoutParams(params);
minimalKSnack.show();

Github 库链接onurkagan / KSnack

于 2020-03-26T10:22:48.013 回答
0

在 Kotlin 中找到了解决方案,试一试:)

 val mainLayout: ConstraintLayout = root.findViewById(R.id.main_layout)

 val button: Button = root.findViewById(R.id.button)
 button.setOnClickListener {

     val snackBar = TSnackbar.make(mainLayout, "Snacking with VectorDrawable", TSnackbar.LENGTH_INDEFINITE)
     snackBar.setActionTextColor(Color.WHITE)
     snackBar.setIconLeft(R.drawable.ic_announcement_black_24dp, 24f)
     val snackBarView = snackBar.view

     val tv = TypedValue()
     if (activity!!.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
         val actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, resources.displayMetrics)
         val params = view!!.getLayoutParams() as FrameLayout.LayoutParams
         params.setMargins(0, actionBarHeight, 0, 0)
         params.height = 70
         snackBarView.setLayoutParams(params)
     }

     snackBarView.setBackgroundColor(Color.parseColor("#CC00CC"))
     val textView = snackBarView.findViewById(com.androidadvance.topsnackbar.R.id.snackbar_text) as TextView
     textView.setTextColor(Color.YELLOW)
     snackBar.setIconPadding(10)
     snackBar.show()
 }
于 2019-10-02T11:59:44.447 回答
0

这对我有用,为您的小吃店设置上边距,考虑您的工具栏大小:

View view = snack.getView();
    CoordinatorLayout.LayoutParams params2 =(CoordinatorLayout.LayoutParams)view.getLayoutParams();
    params2.setMargins(0,50,0,0);
于 2017-10-10T06:41:42.287 回答
0

使用处理程序的 PopupWindow 作为

if(!topPopupWindow.isShowing()){
    topPopupWindow.showAsDropDown(toolbar);
}
handler.removeMessages(0);
handler.sendEmptyMessageDelayed(0,2500);
于 2016-03-29T09:46:55.737 回答