Android Custom Snackbar: can't remove margins

断了今生、忘了曾经 提交于 2019-12-23 19:26:23

问题


Regardless of whether or not it is a good idea to create a custom Snackbar, I have a custom Snackbar and I can't seem to get rid of the margins. I've tried several things such as adjusting in code and in layout as shown below. Nothing seems to work.

I am using the approach laid out by Yakiv here:

final CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) snackBarView.getLayoutParams();
params.setMargins(0,0,0,0);

and other approaches such as

android:layout_marginLeft="0dp"
android:layout_marginStart="0dp"
android:paddingLeft="0dp"
android:paddingStart="0dp" ...

The Snackbar:

My layout file:

<view
xmlns:android="http://schemas.android.com/apk/res/android"
class="android.support.design.internal.SnackbarContentLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/realBlack"
android:layout_marginLeft="0dp"
android:layout_marginStart="0dp"
android:paddingLeft="0dp"
android:paddingStart="0dp">

<TextView
    android:id="@+id/snackbar_text"
    android:layout_width="wrap_content"
    android:layout_height="60dp"
    android:layout_weight="1"
    android:paddingTop="@dimen/design_snackbar_padding_vertical"
    android:paddingBottom="@dimen/design_snackbar_padding_vertical"
    android:paddingLeft="@dimen/design_snackbar_padding_horizontal"
    android:paddingRight="@dimen/design_snackbar_padding_horizontal"
    android:textAppearance="@style/TextAppearance.Design.Snackbar.Message"
    android:maxLines="@integer/design_snackbar_text_max_lines"
    android:layout_gravity="center_vertical|left|start"
    android:ellipsize="end"
    android:textAlignment="viewStart"
    android:text="get some"/>

<Button
    android:id="@+id/snackbar_action"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/design_snackbar_extra_spacing_horizontal"
    android:layout_marginStart="@dimen/design_snackbar_extra_spacing_horizontal"
    android:layout_gravity="center_vertical|right|end"
    android:minWidth="48dp"
    android:visibility="gone"
    android:textColor="?attr/colorAccent"
    style="?attr/borderlessButtonStyle"/>
</view>

The calling code:

        _firmwareSnackbar = CustomSnackbar.make((ViewGroup) _rootView, Snackbar.LENGTH_INDEFINITE);

        _firmwareSnackbar.setText(_flipper.getContext().getString(R.string.settings_firmware_available));

        _firmwareSnackbar.setAction(_flipper.getContext().getString(R.string.settings_firmware_start), new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                _firmwareSnackbar.dismiss();
                _firmwareSnackbar = null;

            }
        });

        _firmwareSnackbar.show();

回答1:


In dimens.xml.

Use this:

 <dimen name="design_snackbar_padding_horizontal">0dp</dimen>

But remember that this will get applied to all the snackbars in your application.




回答2:


Before showing the Snackbar you can remove the parent layout padding in the following way:

//...
_firmwareSnackbar.getView().setPadding(0,0,0,0);
_firmwareSnackbar.show();



回答3:


Add to theme of App or Activity <item name="snackbarStyle">@style/Widget.Design.Snackbar</item>



来源:https://stackoverflow.com/questions/43598890/android-custom-snackbar-cant-remove-margins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!