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

问题描述 投票:0回答:1

我有一个具有自定义布局的自定义小吃店。但是它不能正确显示。它还在后台显示默认的小吃店:下面是我的代码:

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

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

enter image description here

[我已经研究了许多关于Custom Snackbar doesn't work properlySnackbar from custom class not showing的流程问题,但是这些解决方案对我不起作用。

任何想法或见解都会很有帮助。

android android-custom-view android-snackbar
1个回答
0
投票

覆盖以下值

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

Snackbar snackbar = Snackbar.make(mainView, text, Snackbar.LENGTH_LONG);
    View snackBarView = snackbar.getView();
    snackBarView.setBackgroundColor(context.getResources().getColor(R.color.btn_background_color));
    snackbar.show();
© www.soinside.com 2019 - 2024. All rights reserved.