簡體   English   中英

更改快餐欄中操作按鈕的背景顏色

[英]Change the background color of action button in snackbar

如何更改快捷欄中操作按鈕的背景顏色或使其消失(灰色背景)?

在此處輸入圖片說明

我使用這個代碼:

        Snackbar mysnack = Snackbar.make(main_layout, getResources().getString(R.string.snack_1), 5000);
            View view = mysnack.getView();
            TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
            tv.setTextColor(getResources().getColor(R.color.text_light));
            mysnack.setActionTextColor(getResources().getColor(R.color.text_light));
            mysnack.setAction("RATE", new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Uri uri = Uri.parse(getResources().getString(R.string.snack_url));
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                }
            });
            TypedValue typedValue = new TypedValue();
            getTheme().resolveAttribute(R.attr.colorPrimaryDark, typedValue, true);
            final int color = typedValue.data;
            mysnack.getView().setBackgroundColor(color);
            mysnack.show();

我的問題不重復。 我要求背景顏色而不是文本顏色。 首先我們閱讀,然后我們理解,然后我們思考,然后我們決定寫下某人的問題是重復的。

我遇到過同樣的問題。 發現它可能是新材料主題的錯誤。 我的申請的主題是:

<style name="AppTheme" parent="@style/Theme.MaterialComponents.Light.NoActionBar">

如果我將其更改為 AppCompat 小吃按鈕的灰色背景消失。 最終我發現這是因為錯誤。

我的解決方案是(我需要 Material 主題,不能簡單地將其更改為 AppCompat):找到小吃的按鈕 ID。 它是“@id/snackbar_action”:

val snackButton: Button = yourSnackbar.getView().findViewById(R.id.snackbar_action)

然后將其背景更改為空:

snackButton.setBackground(null)

此代碼片段可能會幫助您:

Snackbar snackbar = Snackbar.make(
                    coordinatorLayout,
                    "Snackbar: floatingActionButton1 (normal) clicked",
                    Snackbar.LENGTH_LONG);
            snackbar.setActionTextColor(Color.RED);
            View snackbarView = snackbar.getView();
            snackbarView.setBackgroundColor(Color.WHITE);
            TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
            textView.setTextColor(Color.BLUE);

參考點擊這里

對於 Xamarin,你可以試試這個

 snackbar.View.SetBackgroundColor(Android.Graphics.Color.ParseColor("#32CD32"));

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

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.setActionTextColor(getResources().getColor(R.color.colorAccent));

通過材料組件庫,您可以在應用程序主題中使用snackbarButtonStyle屬性。
就像是:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
    <!-- Style to use for action button within a Snackbar in this theme. -->
    <item name="snackbarButtonStyle">@style/Widget.App.Button.TextButton.Snackbar</item>
</style>


<style name="Widget.App.Button.TextButton.Snackbar" parent="Widget.MaterialComponents.Button.TextButton.Snackbar">
    <item name="backgroundTint">@color/colorSecondary</item>
</style>

在此處輸入圖片說明

暫無
暫無

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

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