Add margins to Snackbar view

前端 未结 17 1451
时光取名叫无心
时光取名叫无心 2020-12-06 16:20

I\'m updating my current app to use snackbars, in the Google spec they show various ways of using them http://www.google.com/design/spec/components/snackbars-toasts.html#sna

相关标签:
17条回答
  • 2020-12-06 16:58

    None of the above solutions worked for me. This is my solution, my idea is create the background with transparent side, so looks like a margin. Try this works for me:

    1. Create drawable for background

       <?xml version="1.0" encoding="utf-8"?>
       <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
           <item>
               <shape android:shape="rectangle">
                   <solid android:color="@color/transparent" />
               </shape>
           </item>
           <item android:top="16dp" android:bottom="16dp" android:left="16dp" android:right="16dp">
               <shape android:shape="rectangle">
                   <solid android:color="#323232" />
                   <corners android:radius="4dp" />
               </shape>
           </item>
       </layer-list>
      
    2. Using kotlin extension to show Snackbar and add the padding

       private fun Snackbar.show(context: Context) {
           this.view.setPadding(Dpx.dpToPx(16), Dpx.dpToPx(16), Dpx.dpToPx(16), Dpx.dpToPx(16))
           this.view.background = context.getDrawable(R.drawable.bg_snackbar)
           show()
       }
      
    3. Call Snackbar make

       Snackbar.make(view, R.string.my_message, Snackbar.LENGTH_SHORT).show(this)
      

    Thats all. Tell me if that works, thanks...

    0 讨论(0)
  • 2020-12-06 16:58

    The other answers did not work for me. I have implemented this instead:

    SnackBar.class

    public  class SnackBar{  
    Context context;
    Snackbar getSnackbar(View v, String s, View.OnClickListener listener, String action){
        Snackbar sn = Snackbar.make(v, "" + s, Snackbar.LENGTH_LONG);
        sn.setAction("" + action, listener);
        sn.setActionTextColor(Color.CYAN);
        sn.show();
     sn.getView().setBackground(ContextCompat.getDrawable(context,R.drawable.snackbar_shape));
        return sn;
    }
    

    in MainActivity.class include

    MainActivity.class:

      // Snackbar.make(v,"Notification for pressing button",Snackbar.LENGTH_LONG).show();
                final SnackBar snackBar=new SnackBar();
               snackBar.context=MainActivity.this;
               snackBar.getSnackbar(v, "Notification for pressing ", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.e(TAG,"cancelled");
                        snackBar.onClick=true;
                        
                    }
                }, "cancel");
    

    in drawable directory

    snackbar_shape.xml

    <item
        android:left="20dp"
        android:right="20dp"
        android:bottom="10dp"
        android:top="10dp">     
        <shape android:shape="rectangle"  >
            <solid android:color="#F31E1E1E" />    
            <padding android:left="25dp"android:right="25dp" android:bottom="10dp"android:top="10dp" />
            <corners android:radius="7dp" />
    
        </shape>
    </item>
    
    0 讨论(0)
  • 2020-12-06 16:59

    In addition to Saeid's answer, you can get the default SnackBar layout params and modify them as you want:

    public static void displaySnackBarWithBottomMargin(Snackbar snackbar, int sideMargin, int marginBottom) {
        final View snackBarView = snackbar.getView();
        final CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) snackBarView.getLayoutParams();
    
        params.setMargins(params.leftMargin + sideMargin,
                    params.topMargin,
                    params.rightMargin + sideMargin,
                    params.bottomMargin + marginBottom);
    
        snackBarView.setLayoutParams(params);
        snackbar.show();
    }
    
    0 讨论(0)
  • 2020-12-06 17:00

    Adding CoordinatorLayout or Frame Layout and then setting margin didn't work for me

    To tackle this problem use Drawable Background where use item to set Margin and shape to set desired Padding

    container_snackbar.xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <!--    Set Margin Here -->
        <item
            android:left="20dp"
            android:right="20dp"
            android:bottom="10dp"
            android:top="10dp">
    
            <!-- Insert your shape here: -->
            <shape android:shape="rectangle"  >
                <solid android:color="#FE4C4C" />
    
                <!-- Padding for inner text-->
                <padding android:left="25dp" android:right="10dp" android:bottom="10dp" android:top="10dp" />
                <corners android:radius="5dp" />
    
            </shape>
        </item>
    </layer-list>
    

    And then from Activity set that Drawable

    MainActivity.java

    Snackbar snack = Snackbar
                     .make(activity,"Hello World                                                                     
    0 讨论(0)
  • 2020-12-06 17:03

    None of the above solutions worked for me.

    But, I got one trick that we can use with the help of translation.

    Snackbar snackbar = Snackbar.make(mActivity.getWindow().getDecorView().getRootView(), message, Snackbar.LENGTH_SHORT);
    View snackBarView = snackbar.getView();
    snackBarView.setTranslationY(-(convertDpToPixel(48, mActivity)));
    snackbar.show();
    

    you can find convertDpToPixel method here

    0 讨论(0)
  • 2020-12-06 17:05

    The key to controlling the Snackbar display is using a android.support.design.widget.CoordinatorLayout Layout. Without it your Snackbar will always be displayed filled at the bottom on small devices and at the bottom left of large devices. Note: You may use the CoordinatorLayout as the root ViewGroup of your layout or anywhere in your layout tree structure.

    After adding it, ensure you are passing the CoordinatorLayout (or child) as the first argument of the Snackbar.make() command.

    By adding padding or margins to your CoordinatorLayout you can control the position and move the Snackbar from the bottom of the screen.

    The material design guidelines specify a minimum and maximum width of the Snackbar. On small devices you will see it fill the width of the screen, while on tablets you will see the Snackbar hit the maximum width and not fill the width of the screen.

    0 讨论(0)
提交回复
热议问题