簡體   English   中英

從樣式更改小吃店的動作顏色

[英]Change action color of snackbar from style

我需要AppTheme.colorAccent為棕色,但我需要我的Snackbar動作顏色為藍色。 如何在不更改AppTheme.colorAccent情況下從樣式更改Snackbar操作按鈕顏色?

我試過這段代碼,但它不起作用:

<style name="TextAppearance.Design.Snackbar" parent="android:TextAppearance" tools:override="true">
    <item name="colorAccent">#3097ff</item>
</style>

使用材料組件庫,您可以做到。

只需在您的主題應用程序中添加snackbarButtonStyle屬性。

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

然后定義您的自定義樣式:

  <style name="snackbar_button" parent="@style/Widget.MaterialComponents.Button.TextButton.Snackbar">
      <item name="backgroundTint">@color/secondaryLightColor</item>
      <item name="android:textColor">@color/primaryDarkColor</item>
  </style>

在此處輸入圖片說明

它需要庫的 1.1.0 版。

您可以定義在顏色colors.xml ,並在使用它snackbar ,如下所示:

val mySnackbar = Snackbar.make(findViewById(R.id.container),"Item added to cart.", Snackbar.LENGTH_SHORT)
mySnackbar.setAction("view cart", View.OnClickListener {/*action to be triggered*/  })
mySnackbar.setActionTextColor(/*color defined*/)
mySnackbar.show()

我在 Kotlin 中實現了這個。

您使用以下代碼為小吃店設置背景顏色和動作文本顏色..

 btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar snackbar = Snackbar.make(
                    view,
                    "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);

            snackbar.setAction("OK", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(
                            getApplicationContext(),
                            "snackbar OK clicked",
                            Toast.LENGTH_LONG).show();
                }
            });

            snackbar.show();
        }
    });

暫無
暫無

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

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