簡體   English   中英

Snackbar 操作文本顏色未更改

[英]Snackbar action text color not changing

我想更改小吃欄的操作文本顏色,但由於某種原因它不起作用。

我使用以下代碼來顯示一個小吃店:

Snackbar.make(findViewById(R.id.root), "text", Snackbar.LENGTH_LONG).setActionTextColor(R.color.yellow).setAction("OK", new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    }
}).show();

setActionTextColor的參數是表示顏色的int ,而不是資源 ID。

取而代之的是:

.setActionTextColor(R.color.yellow)

嘗試:

.setActionTextColor(Color.YELLOW)

如果您無論如何都想使用資源,請嘗試:

.setActionTextColor(ContextCompat.getColor(context, R.color.color_name));

注意:要使用 ContextCompat,我假設您已經在build.gradle文件中包含了 Support 庫(如果您也已經擁有 appcompat (v7) 庫,則它是可選的)。

.setActionTextColor(getResources().getColor(R.color.red))

而不僅僅是

.setActionTextColor(R.color.red)

以上答案都沒有幫助我。 我找到了這個解決方案,它的工作原理是手動更改 TextView 的文本顏色

Snackbar snack = Snackbar.make(v, "Snackbar message", Snackbar.LENGTH_LONG);
View view = snack.getView();
TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);
snack.show();

如果要更改操作按鈕文本顏色..

snackbar.setActionTextColor(getResources().getColor(R.color.colorAccent));

如果要更改操作按鈕背景顏色..

View sbView = snackbar.getView();
Button button=
(Button) sbView.findViewById(com.google.android.material.R.id.snackbar_action);
button.setBackgroundColor(getResources().getColor(R.color.white));

嘗試這個,

 Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "Permission required!", 3000 /*Snackbar.LENGTH_INDEFINITE*/);
 snackbar.setAction("OK", new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // perform any action when the button on the snackbar is clicked
            Toast.makeText(MainActivity.this, "Permission granted.", Toast.LENGTH_SHORT).show();
        }
    });
 snackbar.setBackgroundTint(getResources().getColor(R.color.black));      // set the background tint color for the snackbar
 snackbar.setActionTextColor(getResources().getColor(R.color.purple_500)); // set the action button text color
 snackbar.show();

暫無
暫無

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

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