小吃店不显示 [英] Snack bar not displayed

查看:67
本文介绍了小吃店不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 BaseActivity 继承所有其他活动.

I'm inheriting from BaseActivity for all the other activities.

public class BaseActivity extends AppCompatActivity {

    public static CoordinatorLayout coordinatorLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        coordinatorLayout = (CoordinatorLayout) findViewById(R.id
                .coordinatorLayout1);
    }
}

activity_base.xml

activity_base.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinatorLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.Activity.BaseActivity"> 
</android.support.design.widget.CoordinatorLayout>

尝试从非活动类访问时,不会显示小吃栏.

Snackbar is not displayed when tried to access from a non-activity class.

Snackbar snackbar = Snackbar.make(
        BaseActivity.coordinatorLayout,
        "Helooo....",
        Snackbar.LENGTH_LONG
);

推荐答案

Util类中使用 public 方法,并且不要制作 cordinatorLayout 作为 public static .保留 Activity的实例的 weakReference ,通过它可以显示 SnackBar .方法如下.

Make a public method in a Util class and dont make the cordinatorLayout as public static. Keep the weakReference of your Activity's instance and through that you can show the SnackBar. Method given below.

public void showSnackBar(Activity activity, String message){
    View rootView = activity.getWindow().getDecorView().findViewById(android.R.id.content);
    Snackbar.make(rootView, message, duration).show();
}

这篇关于小吃店不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆