gpt4 book ai didi

android - 如何自定义snackBar 的布局?

转载 作者:IT老高 更新时间:2023-10-28 13:09:41 27 4
gpt4 key购买 nike

有什么方法可以将snackBar的布局改为自定义View吗?

现在它变成黑色了,我们可以更改背景颜色。但我不知道如何为新布局充气并将其作为snackBars 背景?

谢谢...

最佳答案

Snackbar 不允许您设置自定义布局。但是,正如 Primoz990 建议的那样,您可以获得 Snackbar 的 View 。 getView 函数返回 Snackbar.SnackbarLayout,它是一个水平 LinearLayout 对象,其子对象是 TextView 和 Button。要将自己的 View 添加到 Snackbar,您只需隐藏 TextView,并将您的 View 添加到 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();

关于android - 如何自定义snackBar 的布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32453946/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com