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

时间:2015-12-01 12:50:46

标签: android material-design android-dialogfragment 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();

As u can see my snackbars background color showing white color

我正在将对话片段的视图传递给小吃吧。我想要背景颜色为黑色?我怎样才能做到这一点?我在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>

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

17 个答案:

答案 0 :(得分:133)

尝试设置这样的背景颜色:

sbView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.BLACK));

它将100%工作!

答案 1 :(得分:73)

你可以这样做

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();

答案 2 :(得分:16)

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

<color name="design_snackbar_background_color" tools:override="true">@color/colorPrimaryLight</color>

答案 3 :(得分: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();

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

答案 4 :(得分:10)

Kotlin版本(带有extension):

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

fun Snackbar.withColor(@ColorInt colorInt: Int): Snackbar{
   this.view.setBackgroundColor(colorInt)
   return this
}

接下来,在您的活动/片段中,您将能够执行此操作:

Snackbar
  .make(coordinatorLayout, message, Snackbar.LENGTH_LONG)
  .withColor(YOUR_COLOR)
  .show()

答案 5 :(得分: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();

答案 6 :(得分:3)

在使用xamarin android时,我发现ContextCompat.GetColor()返回Int,但setBackgroundColor()需要一个Color类型的Parameter。 所以这就是我如何在我的xamarin android项目中使用它的方法。

Snackbar snackbarview =  Snackbar.Make(toolbar, message, Snackbar.LengthLong);
View snckView = snackbarview.View;                
snckView.SetBackgroundColor(Color.ParseColor(GetString(Resource.Color.colorPrimary)));
snackbarview.Show();

答案 7 :(得分:3)

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

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

提供了您的应用在您的MyAppTheme中使用自定义的调用者AndroidManifest.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">

创建(如果尚未创建)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>

并在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>

答案 8 :(得分:2)

将它放在Utility类中:

public class Utility {
    public static void showSnackBar(Context context, View view, String text) {
        Snackbar sb = Snackbar.make(view, text, Snackbar.LENGTH_SHORT);
        sb.getView().setBackgroundColor(ContextCompat.getColor(context, R.color.colorAccent));
        sb.show();
    }
}

像这样使用:

Utility.showSnackBar(getApplicationContext(), findViewById(android.R.id.content), "Add success!!!");

答案 9 :(得分:2)

我做了一个小类工具,所以我可以轻松地通过应用程序制作自定义彩色零食吧。

part

然后使用它,就像在app中的任何地方一样:

package com.yourapppackage.yourapp;

import android.support.design.widget.Snackbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class SnackbarUtils {

    private int BACKGROUND_COLOR;
    private int TEXT_COLOR;
    private int BUTTON_COLOR;
    private String TEXT;


    public SnackbarUtils(String aText, int aBgColor, int aTextColor, int aButtonColor){
        this.TEXT = aText;
        this.BACKGROUND_COLOR = aBgColor;
        this.TEXT_COLOR = aTextColor;
        this.BUTTON_COLOR = aButtonColor;
    }

    public Snackbar snackieBar(){
        Snackbar snackie = Snackbar.make(MainActivity.getInstance().findViewById(android.R.id.content), TEXT, Snackbar.LENGTH_LONG);
        View snackView = snackie.getView();
        TextView snackViewText = (TextView) snackView.findViewById(android.support.design.R.id.snackbar_text);
        Button snackViewButton = (Button) snackView.findViewById(android.support.design.R.id.snackbar_action);
        snackView.setBackgroundColor(BACKGROUND_COLOR);
        snackViewText.setTextColor(TEXT_COLOR);
        snackViewButton.setTextColor(BUTTON_COLOR);
        return snackie;
    }
}

答案 10 :(得分:1)

没有其他解决方案真正适合我。如果仅设置Snackbar的背景颜色,则TextView和Button下的布局为默认颜色。如果我设置TextView的背景,则在显示SnackBar之后它会眨一下。而且按钮周围的布局仍为默认颜色。

最后,我发现最好的方法是更改​​TextView父级(SnackbarContentLayout)的背景颜色。现在,整个Snackbar的颜色正确,并且在显示时不会闪烁。

snack = Snackbar.make(view, text, duration)
View view = snack.getView();
view.setBackgroundColor(BACKGROUND_COLOR);
TextView tv = view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(TEXT_COLOR);
((SnackbarContentLayout) tv.getParent()).setBackgroundColor(BACKGROUND_COLOR);

答案 11 :(得分:1)

Snackbar (com.google.android.material.snackbar.Snackbar中包含Material Components Library,只需使用setBackgroundTint方法。

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

enter image description here

答案 12 :(得分:1)

我不知道为什么在我的项目中找不到setBackgroundColor()。这就是为什么我创建了扩展函数,现在很好。

fun View.showSnackBar(message: String) {
    val snackBar = Snackbar.make(this, message, Snackbar.LENGTH_LONG)
    snackBar.setBackgroundTint(ContextCompat.getColor(this.context, R.color.colorAccent))
    snackBar.show()
}

像波纹管一样叫它

activity_login.xml

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/login_holder_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   // your UI

</FrameLayout>

LoginActivity.kt

login_holder_layout.showSnackBar("Invalid Email") 

答案 13 :(得分:0)

public class CustomBar {

public static void show(View view, String message, boolean isLong) {
    Snackbar s = Snackbar.make(view, message, isLong ? Snackbar.LENGTH_LONG : Snackbar.LENGTH_SHORT);
    s.getView().setBackgroundColor(ContextCompat.getColor(view.getContext(), R.color.red_900));
    s.show();
}

public static void show(View view, @StringRes int message, boolean isLong) {
    Snackbar s = Snackbar.make(view, message, isLong ? Snackbar.LENGTH_LONG : Snackbar.LENGTH_SHORT);
    s.getView().setBackgroundColor(ContextCompat.getColor(view.getContext(), R.color.red_900));
    s.show();
}

}

答案 14 :(得分:0)

setBackgroundResource()也一样。

Snackbar snackbar = Snackbar.make(view, text, Snackbar.LENGTH_LONG);
View sbView = snackbar.getView();
sbView.setBackgroundResource(R.color.background);
snackbar.show();

答案 15 :(得分:0)

基本上,提供的解决方案有一个缺点。 它们改变了小吃条的形状并去除了半径。

个人而言,喜欢这样的东西

val snackbar = Snackbar.make(view, text, Snackbar.LENGTH_LONG);
val view = snackbar.getView();
val color = view.resources.getColor(colorId)
view.background.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)

答案 16 :(得分:-1)

<块引用>

您可以在材料设计库上使用此代码

你可以使用的方式

创建颜色代码

打开 res/values/colors.xml 并添加这一行

<resources>
    <color name="custom_color_name">CustomCode</color>
</resources>

创建 Snackbar 并更改背景

打开您的 Activity 或 Fragment 并创建 Snackber

Snackbar snackbar= Snackbar.make(root,R.string.imageUploadTitle_error, BaseTransientBottomBar.LENGTH_LONG);

获取 Snackbar 视图

现在您应该获得 SnackbarView 并在其中更改自定义背景

View snackview = snackbar.getView();

改变背景颜色

用这个函数设置snackbar的背景颜色

snackview.setBackgroundColor(ContextCompat.getColor(getActivity() , R.color.error));

显示此小吃店

现在应该显示 Snackbar

snackbar.show();

因此您可以看到更改的背景为自定义颜色

相关问题