簡體   English   中英

如何更改小吃店的背景顏色?

[英]How to change background color of the snackbar?

我在警報對話框的正面觸摸中的snackbar中顯示小吃DialogFragment 這是我的代碼片段:

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

如您所見,我的小吃店背景顏色顯示為白色

我將DialogFragment的視圖傳遞給小吃店。 我希望背景顏色為黑色。 我怎樣才能做到這一點? 我在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>

雖然我將對話框的背景顏色設置為白色,但它應該通過將背景顏色設置為snapbar來覆蓋。

嘗試像這樣設置背景顏色:

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

它將 100% 工作!

你可以這樣做

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

由於其他答案均未提供自定義樣式覆蓋(我認為這是最安全的更新方式之一),因此我在此處發布了我的解決方案。

我發布了一個已經解決了新的AndroidXsupport design 28 )主題的解決方案。

假設您的應用程序在您的AndroidManifest.xml中使用名為MyAppTheme的自定義它們:

<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 in res/values -->
<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>

2020 年更新

由於上述解決方案刪除了​​小吃店的圓角,因為以這種方式設置背景使用了傳統的小吃店設計,如果您想保留材料設計,您可以。

  1. 如果您的目標是 API 21+

android:background替換 android android:backgroundTint

<!-- snackbar style in res/values-21/ -->
<style name="MySnackBarStyle" parent="Widget.MaterialComponents.Snackbar">
    <item name="android:backgroundTint">@color/mySnackbarBackgroundColor</item>
</style>
  1. 如果您的目標是 API < 21,那么如果您決定使用 API < 21 的舊式快餐欄,您可以在res/values-21/文件夾中設置您的MySnackbarStyle並將之前的 - legacy - 樣式保留在res/values文件夾中。

  2. 如果您的目標是 API < 21,並且您希望在這個較低的 API 級別中也擁有小吃吧的材質樣式,您可以通過以下方式更改您的res/values/中的小吃吧風格:

<!-- snackbar style in res/values/ -->
<style name="MySnackBarStyle" parent="Widget.MaterialComponents.Snackbar">
    <item name="android:background">@drawable/my_snackbar_background</item>
</style>

並從官方 repo借用你的my_snackbar_background ,這樣:

<!-- in res/drawable/ -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="4dp"/>
    <solid android:color="@color/mySnackbarBackgroundColor"/>
</shape>

編輯 2022:

如果您只想更改單個快餐欄,而不是跨應用程序,那么您可以使用 ContextThemeWrapper 如下;

ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.CustomSnackbarTheme);
customSnackBar = Snackbar.make(ctw, view, "", Snackbar.LENGTH_LONG);

並在您的樣式文件中

<style name="CustomSnackbarTheme">
    <item name="snackbarStyle">@style/MySnackBarStyle</item>
</style>

<style name="MySnackBarStyle" parent="Widget.MaterialComponents.Snackbar">
    <item name="android:background">@android:color/red</item>
</style>

這是一個游樂場回購

在此處輸入圖像描述

Kotlin 版本(帶有擴展名):

在文件(例如 SnackbarExtension.kt)中創建擴展名:

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

接下來,在您的 Activity/Fragment 中,您將能夠執行此操作:

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

如果要為所有 Snackbars 定義背景顏色,只需覆蓋資源中某處的design_snackbar_background_color值。 例如:

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

波紋管代碼對於更改消息的文本顏色很有用。

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

第二種方式:您也可以通過更改活動主題來更改顏色。

使用材質組件庫只需使用setBackgroundTint方法。

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

在此處輸入圖像描述


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

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

然后只需使用它:

scope.launch {scaffoldState.snackbarHostState.showSnackbar("Snackbar text")}

在此處輸入圖像描述

為時已晚,但萬一有人仍然需要幫助。 這是工作解決方案。

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

我做了一個小工具類,所以我可以很容易地通過應用程序制作自定義顏色的小吃店。

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;
    }
}

然后使用它,就像在應用程序中的任何位置一樣:

new SnackbarUtils("This is the text displayed", Color.RED, Color.BLACK, Color.YELLOW).snackieBar().setAction("OTAY", v -> { 
     //donothing
     }).show();

在使用 xamarin android 時,我發現 ContextCompat.GetColor() 返回 Int 但 setBackgroundColor() 需要顏色類型的參數。 所以這就是我如何讓它在我的 xamarin android 項目中工作。

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

把它放在一個實用程序類中:

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!!!");

沒有其他解決方案真正適合我。 如果我只設置 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);

setBackgroundResource()也同樣有效。

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

基本上,所提供的解決方案有一個缺點。 他們改變了小吃店的形狀並移除了半徑。

就個人而言,更喜歡這樣的東西

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

我不知道為什么在我的項目中找不到 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") 

對於科特林:

Snackbar.make(view,"simple text",Snackbar.LENGTH_SHORT).setBackgroundTint(Color.RED).show()
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();
}

}

您可以在材料設計庫中使用此代碼

你可以使用這種方式

創建顏色代碼

打開 res/values/colors.xml 並添加這一行

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

創建 Snackbar 並更改背景

打開您的活動或片段並創建 Snackber

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

獲取 Snackbar 視圖

現在你應該得到 SnackbarView 並在其中更改自定義背景

View snackview = snackbar.getView();

改變背景顏色

使用此功能設置小吃店背景顏色

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

顯示此小吃店

現在應該顯示 Snackbar

snackbar.show();

因此您可以看到將背景更改為自定義顏色

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM