50

Tôi đang hiển thị snackbar trong DialogFragment Trong tích cực click của alertDialog. Đây là đoạn mã của tôi.Làm cách nào để thay đổi màu nền của thanh snack?

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

tôi đi qua điểm của dialogfragment đến snackbar. Tôi muốn màu nền đen? Tôi có thể làm cái này như thế nào? Tôi đang trả về alertDialog trong DialogFragment. Và chủ đề tôi đang đặt cho hộp thoại như sau là

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

Mặc dù tôi đặt màu nền thành trắng cho hộp thoại, nó sẽ ghi đè bằng cách đặt màu nền cho thanh snack.

+2

[http://www.technotalkative.com/part-3-styling-snackbar/](http://www.technotalkative.com/part-3-styling-snackbar/) –

+0

cố gắng đó đã không giúp tôi ... tôi đang gọi thanh snack từ đoạn hộp thoại + alertDialog trong đó và tôi đang chuyển sang chế độ xem nút bấm tích cực cho thanh snack – Ajinkya

Trả lời

90

Hãy thử thiết lập màu nền như thế này:

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

Nó sẽ làm việc 100%!

+26

bạn có thể cần phải thực hiện 'snackBarView.getView(). SetBackgrondColor (ContextCompat.getColor (getActivity(), R.color.BLACK)); ' – jaytj95

+0

Nó hoạt động như là nó phải như thế. – Klevi01

+0

Nếu bạn tìm thấy trang này từ Google và giải pháp trên không hiệu quả với bạn, bạn có thể cần phải thử trang này thay vì: 'sbView.setBackgroundColor (getResources(). GetColor (R.color.BLACK))' – modu

58

bạn có thể làm điều đó như thế này

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

vì bạn có thể thấy tôi đã làm chính xác điều tương tự nhưng không hiển thị màu đen – Ajinkya

+0

tôi có sử dụng tương tự trong một dự án của tôi, cố gắng hiển thị nó trong hoạt động để thử nghiệm, có thể nó không hoạt động do hộp thoại –

+0

ya hoạt động của nó trên hoạt động nhưng tôi muốn nó trên đoạn hộp thoại. – Ajinkya

11

Dưới đây là mã hữu ích để thay đổi màu văn bản của tin nhắn.

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

Way Thứ hai: Bạn có thể thay đổi màu sắc bằng cách thay đổi chủ đề của hoạt động cũng có.

1

Tôi đã tạo một lớp utils nhỏ để tôi có thể dễ dàng tạo các thanh snack màu tùy chỉnh thông qua ứng dụng.

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

sau đó sử dụng nó, như thế này bất cứ nơi nào trên ứng dụng:

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

Đặt nó trong một lớp 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(); 
    } 
} 

Sử dụng như thế này:

Utility.showSnackBar(getApplicationContext(), findViewById(android.R.id.content), "Add success!!!"); 
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(); 
} 

}

1

Quá muộn nhưng trong trường hợp ai đó vẫn cần trợ giúp. Đây là giải pháp làm việc.

 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(); 
Các vấn đề liên quan