如何在 Android Studio 中更改 Snackbar 背景文本颜色?

发布于 2025-01-15 10:17:43 字数 1296 浏览 0 评论 0原文

我的 Snackbar 背景显示我在 Theme.xml 上设置的默认背景。那么,如何更改 SnackBar 的背景颜色呢?

任何帮助将不胜感激。

输入图片此处描述

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    binding = ActivityNewDeltioBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());

    SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
    ViewPager viewPager = binding.viewPager;
    viewPager.setAdapter(sectionsPagerAdapter);
    TabLayout tabs = binding.tabs;
    tabs.setupWithViewPager(viewPager);
    FloatingActionButton fab = binding.fab;

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar message = Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null);
            message.show();
        }
    });
}

编辑: 我也尝试过创建自定义样式,但没有达到预期效果。它改变了 SnackBar 侧面的颜色,而不是背景本身... ibb.co/p1zqNqd

My Snackbar background displays the default background I've set on the Theme.xml. So, how do I change the background color of the SnackBar?

Any help would be much appreciated.

enter image description here

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    binding = ActivityNewDeltioBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());

    SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
    ViewPager viewPager = binding.viewPager;
    viewPager.setAdapter(sectionsPagerAdapter);
    TabLayout tabs = binding.tabs;
    tabs.setupWithViewPager(viewPager);
    FloatingActionButton fab = binding.fab;

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar message = Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null);
            message.show();
        }
    });
}

Edit:
I've also tried creating a custom style, but that didn't work as expected. It changed the color of the SnackBar sides, not the background itself... ibb.co/p1zqNqd

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撑一把青伞 2025-01-22 10:17:43

您可能需要尝试 message.setBackgroundColor(colorYouWant)

另外,如果您有colors.xml,我会说要确保您正确命名颜色。

如需了解更多信息,您还可以查看此答案

You might have to try message.setBackgroundColor(colorYouWant).

Also, if you have a colors.xml, I would say to make sure that you're correctly naming the colors.

For more information, you could also check out this answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文