【问题标题】:Is it possible to add some buttons or other different View's to Snackbar?是否可以向 Snackbar 添加一些按钮或其他不同的视图?
【发布时间】:2017-01-24 16:17:54
【问题描述】:

如果可能的话,你能告诉我我该怎么做吗?否则,请告诉我可以用什么来代替 Snackbar?

【问题讨论】:

标签: android android-coordinatorlayout


【解决方案1】:

你可以这样做。但是,我不建议使用此解决方法。通常你不需要在snackbar 中放置自定义布局。 Snackbar 是作为所有应用通知某事的统一方式而开发的。

 CoordinatorLayout coordinatorLayout = (CoordinatorLayout) findViewById(R.id.main_content);//coordimator layout where you use snackbar
            snackbar = Snackbar.make(coordinatorLayout, "", Snackbar.LENGTH_INDEFINITE);
            Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();//get the snackbar's view
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View snackView = inflater.inflate(R.layout.custom_layout, null);
            snackView.setBackgroundColor(Color.WHITE);
            layout.addView(snackView, 0);//add our custom view to snackbar
            snackbar.show();

example custom snackbar

【讨论】:

  • 如何通过xml实现?
  • 我不知道 xml 方法。但是您可以创建 xml 自定义视图并通过充气机添加此视图。我在上面展示了这个方法
猜你喜欢
  • 1970-01-01
  • 2015-10-09
  • 2011-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-02
  • 1970-01-01
相关资源
最近更新 更多