[英]Change the background color of action button in snackbar
how can I change the background color of action button in snackbar or make it dissapear (grey background)?如何更改快捷栏中操作按钮的背景颜色或使其消失(灰色背景)?
i use this code:我使用这个代码:
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();
and my question is not duplicate.我的问题不重复。 I ask for BACKGROUND color and not the text color.我要求背景颜色而不是文本颜色。 First we read, then we understand, then we think and then we decide to write that someone's question is a duplicate.首先我们阅读,然后我们理解,然后我们思考,然后我们决定写下某人的问题是重复的。
I had the same issue.我遇到过同样的问题。 Found out it can be a bug of new Material theme.发现它可能是新材料主题的错误。 Main theme of my application is:我的申请的主题是:
<style name="AppTheme" parent="@style/Theme.MaterialComponents.Light.NoActionBar">
If I change it to AppCompat snack button's gray background dissapears.如果我将其更改为 AppCompat 小吃按钮的灰色背景消失。 Eventually I found that it was because of the bug.最终我发现这是因为错误。
My solution was (I needed Material theme and can not simply change it to AppCompat): Found button id for the snack.我的解决方案是(我需要 Material 主题,不能简单地将其更改为 AppCompat):找到小吃的按钮 ID。 It is "@id/snackbar_action":它是“@id/snackbar_action”:
val snackButton: Button = yourSnackbar.getView().findViewById(R.id.snackbar_action)
and then changed it background to null:然后将其背景更改为空:
snackButton.setBackground(null)
This code snippet may help you out:此代码片段可能会帮助您:
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);
for Reference Click Here参考点击这里
对于 Xamarin,你可以试试这个
snackbar.View.SetBackgroundColor(Android.Graphics.Color.ParseColor("#32CD32"));
If you want to change action button background color..如果要更改操作按钮背景颜色..
View sbView = snackbar.getView();
Button button=
(Button) sbView.findViewById(com.google.android.material.R.id.snackbar_action);
button.setBackgroundColor(getResources().getColor(R.color.white));
If you want to change action button text color..如果要更改操作按钮文本颜色..
snackbar.setActionTextColor(getResources().getColor(R.color.colorAccent));
With the Material Components Library you can use the snackbarButtonStyle
attribute in your app theme.通过材料组件库,您可以在应用程序主题中使用snackbarButtonStyle
属性。
Something like:就像是:
<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.