如何改变小吃店的背景颜色?

Aji*_*kya 88 android android-dialogfragment material-design android-snackbar

我在DialogFragment中显示snackbar在alertDialog的正面点击中.这是我的代码片段.

Snackbar snackbar = Snackbar.make(view, "Please enter customer name", Snackbar.LENGTH_LONG)
                .setAction("Action", null);
View sbView = snackbar.getView();
sbView.setBackgroundColor(Color.BLACK);
snackbar.show();
Run Code Online (Sandbox Code Playgroud)

你可以看到我的小吃店背景颜色显示白色

我正在将对话片段的视图传递给快餐栏.我想要背景颜色为黑色?我怎样才能做到这一点?我在DialogFragment中返回alertDialog.我正在设置对话框的主题如下

<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">

    <!-- Used for the buttons -->
    <item name="colorAccent">@color/accent</item>
    <!-- Used for the title and text -->
    <item name="android:textColorPrimary">@color/primary</item>
    <!-- Used for the background -->
    <item name="android:background">@color/white</item>
</style>
Run Code Online (Sandbox Code Playgroud)

虽然我将背景颜色设置为白色以进行对话,但是应该通过将背景颜色设置为快餐栏来覆盖它.

Dus*_*vic 146

尝试设置背景颜色如下:

sbView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.BLACK));
Run Code Online (Sandbox Code Playgroud)

它将100%工作!

  • 你可能需要做`snackBarView.getView().setBackgrondColor(ContextCompat.getColor(getActivity(),R.color.BLACK));` (43认同)
  • 如果您发现来自 Google 的这个页面和以上解决方案对您不起作用,您可能需要尝试这个:`sbView.setBackgroundColor(getResources().getColor(R.color.BLACK))` (3认同)

Zub*_*ber 78

你可以这样做

Snackbar snackbar;
snackbar = Snackbar.make(view, "Message", Snackbar.LENGTH_SHORT);
View snackBarView = snackbar.getView();
snackBarView.setBackgroundColor(yourColor);
TextView textView = (TextView) snackBarView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(textColor);
snackbar.show();
Run Code Online (Sandbox Code Playgroud)


4em*_*dan 16

如果要为所有Snackbars定义背景颜色,只需覆盖design_snackbar_background_color资源中的某个值即可.例如:

<color name="design_snackbar_background_color" tools:override="true">@color/colorPrimaryLight</color>
Run Code Online (Sandbox Code Playgroud)


Kai*_*ade 12

Bellow代码对于更改消息的文本颜色很有用.

Snackbar snackbar = Snackbar.make(rootView, "Enter Your Message",Snackbar.LENGTH_SHORT);
View view = snackbar.getView();
TextView tv = (TextView)view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.RED);
snackbar.show();
Run Code Online (Sandbox Code Playgroud)

第二种方式:您也可以通过改变活动主题来改变颜色.


Phi*_*hil 12

Kotlin版(带扩展名):

在文件中创建(例如SnackbarExtension.kt)扩展名:

fun Snackbar.withColor(@ColorInt colorInt: Int): Snackbar{
   this.view.setBackgroundColor(colorInt)
   return this
}
Run Code Online (Sandbox Code Playgroud)

接下来,在您的Activity/Fragment中,您将能够执行此操作:

Snackbar
  .make(coordinatorLayout, message, Snackbar.LENGTH_LONG)
  .withColor(YOUR_COLOR)
  .show()
Run Code Online (Sandbox Code Playgroud)


sha*_*eep 10

由于没有其他答案提供自定义样式覆盖(我认为这样做是更安全的更新方式之一),因此我在此处发布了解决方案。

我发布了一个已经解决了新AndroidXsupport design 28)主题的解决方案。

前提是你的应用程序中使用自定义它们卡莱MyAppThemeAndroidManifest.xml

<application
        android:name=".MyApplicationName"
        android:allowBackup="true"
        android:icon="@mipmap/icon"
        android:roundIcon="@mipmap/icon_round"
        android:label="@string/app_name"
        android:theme="@style/MyAppTheme">
Run Code Online (Sandbox Code Playgroud)

创建(如果还没有)values/style.xml覆盖应用程序使用的主题的文件:

<style name="MyAppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="colorPrimary">@color/myColorPrimary</item>
    <item name="colorPrimaryDark">@color/myColorPrimaryDark</item>
    <item name="colorAccent">@color/myColorAccent</item>
    <item name="snackbarStyle">@style/MySnackBarStyle</item>
</style>

<!-- snackbar style -->
<style name="MySnackBarStyle" parent="Widget.MaterialComponents.Snackbar">
    <item name="android:background">@color/mySnackbarBackgroundColor</item>
</style>
Run Code Online (Sandbox Code Playgroud)

并在values/colors.xml文件中提供颜色

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="myColorPrimary">#008577</color>
    <color name="myColorPrimaryDark">#00574B</color>
    <color name="myColorAccent">#D81B60</color>
    <color name="mySnackbarBackgroundColor">#D81B60</color>
</resources>
Run Code Online (Sandbox Code Playgroud)

  • 这是最干净,最好的解决方案 (2认同)

Nou*_*far 5

现在为时已晚,万一有人仍然需要帮助.这是工作解决方案.

      Snackbar snackbar = Snackbar.make(mainView, text, Snackbar.LENGTH_LONG);
    View snackBarView = snackbar.getView();
    snackBarView.setBackgroundColor(context.getResources().getColor(R.color.btn_background_color));
    snackbar.show();
Run Code Online (Sandbox Code Playgroud)


Gab*_*tti 5

使用材料组件库只需使用该setBackgroundTint方法。

    Snackbar snackbar = Snackbar.make(view, "Snackbar custom style", Snackbar.LENGTH_LONG);
    snackbar.setBackgroundTint(ContextCompat.getColor(this,R.color.secondaryColor));
    snackbar.show();
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


使用Jetpack Compose,您可以自SnackbarHost定义定义自定义Snackbar

    snackbarHost = {
        // reuse default SnackbarHost to have default animation and timing handling
        SnackbarHost(it) { data ->
            Snackbar(
                snackbarData = data,
                backgroundColor = Color.Red
            )
        }
    },
Run Code Online (Sandbox Code Playgroud)

然后只需使用它:

scope.launch {scaffoldState.snackbarHostState.showSnackbar("Snackbar text")}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明