温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - android: custom snackbar bar displays default snack bar in the background
android android-custom-view android-snackbar

其他 - android:自定义快餐栏在后台显示默认的快餐栏

发布于 2020-04-13 17:47:59

我有一个具有自定义布局的自定义小吃栏,但是无法正确显示,它还在后台显示默认的小吃栏:以下是我的代码:

    import android.support.annotation.NonNull;
    import android.support.design.widget.BaseTransientBottomBar;
    import android.support.design.widget.Snackbar;
    import android.support.v7.widget.AppCompatTextView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    public class CustomSnackbar extends BaseTransientBottomBar<CustomSnackbar> {

    protected CustomSnackbar(@NonNull ViewGroup parent,
                             @NonNull View content, @NonNull 
        android.support.design.snackbar.ContentViewCallback contentViewCallback) {
        super(parent, content, contentViewCallback);
    }

    private static class ContentViewCallback
            implements android.support.design.snackbar.ContentViewCallback {

        // view inflated from custom layout
        private View view;

        public ContentViewCallback(View view) {
            this.view = view;
        }

        @Override
        public void animateContentIn(int delay, int duration) {
            // TODO: handle enter animation
        }

        @Override
        public void animateContentOut(int delay, int duration) {
            // TODO: handle exit animation
        }
    }

    public static CustomSnackbar make(ViewGroup parent, int duration,String text) {
        // inflate custom layout
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.custom_snackbar, parent, false);
        //Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout)view;
        //layout.setPadding(0, 0, 0, 0);//set padding to 0
        AppCompatTextView textView = (AppCompatTextView) view.findViewById(R.id.textview_snackbar_text);
        textView.setText(text);
        // create with custom view
        ContentViewCallback callback= new ContentViewCallback(view);
        CustomSnackbar customSnackbar = new CustomSnackbar(parent, view, callback);
        customSnackbar.setDuration(duration);
        return customSnackbar;
    }
}

以下是我的自定义快餐栏的自定义布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="10dp"
android:padding="0dp"
android:background="@android:color/transparent"
>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/button_green"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    >
<android.support.v7.widget.AppCompatTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textview_snackbar_text"
    android:textColor="@color/white"
    android:text=""
    android:textSize="@dimen/extra_small_text_size"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    />
</LinearLayout>
</android.support.v7.widget.CardView>

这是我从BaseActivity使用自定义小吃栏的方式:我在BaseActivity和android.R.id.content中使用它,以便可以从所有活动中调用自定义小吃栏。

public void showSnackbar(String message) {
        CustomSnackbar.make(findViewById(android.R.id.content),
                CustomSnackbar.LENGTH_LONG,message).show();
}

但是,当我调用此方法时,它显示如下:

在此处输入图片说明

我已经研究了许多关于流程的堆栈问题,例如“ 自定义Snackbar”无法正常工作来自自定义类的“ Snackbar”未显示,但是这些解决方案对我而言不起作用。

任何想法或见解将非常有帮助。

Edit1:如给出的答案中所述;

在我的CustomSnackbar的make方法中:我给出了以下建议:

    view.setBackgroundColor(parent.getContext().getResources().getColor(R.color.design_snackbar_background_color));

它看起来像下面的样子:

在此处输入图片说明

我的cardview的行为现在丢失了:是否有任何方法也可以恢复我的cardview的拐角半径和宽度以最大化!

查看更多

提问者
adi
被浏览
15
Ajeett 2020-02-03 16:05

覆盖以下值

<color name="design_snackbar_background_color" tools:override="true">@color/transparent</color>

要么

    Snackbar snackbar = Snackbar.make((ViewGroup) findViewById(android.R.id.content), "", Snackbar.LENGTH_LONG);
    Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
    TextView textView = (TextView) layout.findViewById(android.support.design.R.id.snackbar_text);
    textView.setVisibility(View.INVISIBLE);
    LayoutInflater mInflater = LayoutInflater.from(getApplicationContext());
    View snackView = mInflater.inflate(R.layout.snackbar, null);
    TextView textViewTop = (TextView) snackView.findViewById(R.id.textview_snackbar_text);
    textViewTop.setText("ssdfds");
    textViewTop.setTextColor(Color.WHITE);
    layout.setPadding(0,0,0,0);
    layout.addView(snackView, 0);
    layout.setBackgroundColor(getResources().getColor(R.color.transparent));
    snackbar.show();

在您的styles.xml中粘贴以下代码

 <style name="MyRoundSnackbar" parent="@style/Widget.MaterialComponents.Snackbar">
        <item name="android:layout_margin">32dp</item>
    </style>

然后在您的主题中粘贴此行

<item name="snackbarStyle">@style/MyRoundSnackbar</item>

还要将此行粘贴到您的xml中

<color name="transparent">#00FFFFFF</color>

在此处输入图片说明