Custom Snackbar doesn't work properly

笑着哭i 提交于 2020-06-01 09:44:57

问题


I'm trying to show a custom Snackbar as in this example:

how to customize snackBar's layout?

This is my custom code to create a Snackbar:

protected void showSnackBar(View view) {

    Snackbar snackbar = Snackbar.make(view, "", Snackbar.LENGTH_INDEFINITE);
    Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
    TextView textView = (TextView) layout.findViewById(android.support.design.R.id.snackbar_text);
    textView.setVisibility(View.INVISIBLE);

    LayoutInflater inflater = LayoutInflater.from(this);

    View snackView = inflater.inflate(R.layout.custom_snackbar, null);

    layout.addView(snackView, 0);

    ViewGroup group = (ViewGroup) snackbar.getView();
    group.setBackgroundColor(ContextCompat.getColor(this, R.color.green_wasabi));

    snackbar.show();

}

My goal is to have a method in my base activity to call from any other activity.

But my problem is when I show the Snackbar, that it is shown under the keyboard:

In addition it does not show all views of layout:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/relativelayout_sync_schedule_runs"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="10dp"
android:background="@color/green_wasabi"
android:layout_alignParentBottom="true"
android:layout_gravity="center_horizontal|bottom">

<ProgressBar
    android:id="@+id/progressbar_infinite"
    android:layout_width="30dp"
    android:layout_height="30dp"
    style="@android:style/Widget.Material.ProgressBar.Small"
    android:layout_centerVertical="true"
    android:layout_marginRight="15dp"
    android:layout_toLeftOf="@+id/textview_sync_runs"
    android:layout_toStartOf="@+id/textview_sync_runs" />


<com.runator.ui.widget.text.CustomTextView
    android:id="@+id/textview_sync_runs"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/import_runs"
    android:textColor="@color/black_midnight"
    style="@style/texts.medium_medium_big"
    app:font_type="bold"
    android:gravity="center"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"/>

If anyone has any suggestions I will be happy to hear it.

Thanks in advance.


回答1:


First of all, you don't need to set background colour programmatically, as you are already doing that in xml files, so remove these lines

ViewGroup group = (ViewGroup) snackbar.getView();
group.setBackgroundColor(ContextCompat.getColor(this, R.color.green_wasabi));

Secondly, try with passing Coordinator Layout of the activity as the argument, instead of Linear/Relative Layout, i.e. add coordinator layout as the parent layout in the activity and pass it.

Check the layout bounds of the coordinator layout in the xml view. It should not extend the bounds of activity.




回答2:


Can you try that code for adding the Snackbar on your Baseactivity.

Snackbar snackbar = Snackbar.make( rootView, text, Snackbar.LENGTH_LONG );
View snackBarView = snackbar.getView();
snackBarView.setBackgroundColor( this.mContext.getResources().getColor( R.color.pepapp_bright_red ) );
snackbar.show();



回答3:


Code below works same as Snackbar, but full of customized (UI, animation, actions...) components.

public class CustomSnackBar extends 
        BaseTransientBottomBar<CustomSnackBar> {

    /**
     * Time duration till the snackbar is visible to user
     */
    public static final int DURATION_SEC_2 = 2000;

    /**
     * Constructor for the transient bottom bar.
     *
     * @param parent The parent for this transient bottom bar.
     * @param content The content view for this transient bottom bar.
     * @param callback The content view callback for this transient bottom bar.
     */
    private CustomSnackBar(ViewGroup parent, View content, ContentViewCallback callback) {
        super(parent, content, callback);
    }

    public static CustomSnackBar make(@NonNull ViewGroup parent, @Duration int duration) {
        final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        final View content = inflater.inflate(R.layout.snackbar_view, parent, false);
        final ContentViewCallback viewCallback = new ContentViewCallback(content);
        final CustomSnackBar customSnackbar = new CustomSnackBar(parent, content, viewCallback);
        customSnackbar.setDuration(duration);
        return customSnackbar;
    }

    public CustomSnackBar setText(CharSequence text) {
        TextView textView = (TextView) getView().findViewById(R.id.snackbar_text);
        textView.setText(text);
        return this;
    }

    public CustomSnackBar setAction(CharSequence text, final View.OnClickListener listener) {
        Button actionView = (Button) getView().findViewById(R.id.snackbar_action);
        actionView.setText(text);
        actionView.setVisibility(View.VISIBLE);
        actionView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onClick(view);
                // Now dismiss the Snackbar
                dismiss();
            }
        });
        return this;
    }

    private static class ContentViewCallback implements BaseTransientBottomBar.ContentViewCallback {

        private View content;

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

        @Override
        public void animateContentIn(int delay, int duration) {
            ViewCompat.setScaleY(content, 0f);
            ViewCompat.animate(content).scaleY(1f).setDuration(duration).setStartDelay(delay);
        }

        @Override
        public void animateContentOut(int delay, int duration) {
            ViewCompat.setScaleY(content, 1f);
            ViewCompat.animate(content).scaleY(0f).setDuration(duration).setStartDelay(delay);
        }
    }
}


来源:https://stackoverflow.com/questions/36479144/custom-snackbar-doesnt-work-properly

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