如何自定义snackBar的布局? [英] how to customize snackBar's layout?

查看:73
本文介绍了如何自定义snackBar的布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么方法可以把snackBar的布局改成自定义View?

Is there any method to change the layout of a snackBar to custom View?

现在变成黑色,我们可以更改背景颜色.但我不知道正确的方法来膨胀一个新的布局并将其作为小吃店的背景?

Now it comes black and we can change the background color. But I don't know the right way to inflate a new layout and making it as snackBars background?

谢谢...

推荐答案

Snackbar 不允许您设置自定义布局.但是,正如 Primoz990 建议的那样,您可以获得 Snackbar 的视图.getView 函数返回 Snackbar.SnackbarLayout,它是一个水平的 LinearLayout 对象,它的子元素是一个 TextView 和一个 Button.要将您自己的视图添加到 Snackbar,您只需隐藏 TextView,并将您的视图添加到 Snackbar.SnackbarLayout.

The Snackbar does not allow you to set a custom layout. However, as Primoz990 suggested you can get the Snackbar's View. The getView function returns the Snackbar.SnackbarLayout, which is a horizontal LinearLayout object whose children are a TextView and a Button. To add your own View to the Snackbar, you just need to hide the TextView, and add your View to the Snackbar.SnackbarLayout.

// Create the Snackbar
Snackbar snackbar = Snackbar.make(containerLayout, "", Snackbar.LENGTH_LONG);
// Get the Snackbar's layout view
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
// Hide the text
TextView textView = (TextView) layout.findViewById(android.support.design.R.id.snackbar_text);
textView.setVisibility(View.INVISIBLE);

// Inflate our custom view
View snackView = mInflater.inflate(R.layout.my_snackbar, null);
// Configure the view
ImageView imageView = (ImageView) snackView.findViewById(R.id.image);
imageView.setImageBitmap(image);
TextView textViewTop = (TextView) snackView.findViewById(R.id.text);
textViewTop.setText(text);
textViewTop.setTextColor(Color.WHITE);

//If the view is not covering the whole snackbar layout, add this line
layout.setPadding(0,0,0,0);

// Add the view to the Snackbar's layout
layout.addView(snackView, 0);
// Show the Snackbar
snackbar.show();

这篇关于如何自定义snackBar的布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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