简体   繁体   English

如果文本很长,如何使快餐栏操作按钮显示在不同的行中?

[英]How to make that the snackbar action button be shown in a different line if the text is long?

I am working with the Material Design components in the support library version 28.0.0.我正在使用支持库版本 28.0.0 中的 Material Design 组件。

I want to show a snackbar that when the text inside the action button is too long it be shown in a different line than the message of the snack bar.我想显示一个小吃店,当操作按钮内的文本太长时,它会显示在与小吃店消息不同的行中。

Following the Material design documentation of these components it seems to be possible using the default snackbar as shown here:遵循这些组件的材料设计文档,似乎可以使用默认的快捷栏,如下所示:

https://material.io/design/components/snackbars.html#implementation https://material.io/design/components/snackbars.html#implementation

But using this code:但是使用此代码:

var snackbar:Snackbar = Snackbar.make(root, message, Snackbar.LENGTH_SHORT)
    snackbar.setAction(action, View.OnClickListener {  })
    snackbar.show()

If the action text is long it doesn't move to the next line.如果操作文本很长,它不会移动到下一行。

The root layout is a CoordinatorLayout.根布局是一个 CoordinatorLayout。

So I do not know what I am missing in the snackbar to make it work.所以我不知道我在小吃店里缺少什么才能让它发挥作用。

Thanks!谢谢!

The problem is actually an issue of android itself, the attribute loading from the default android dimen file is broken so the required attribute that is used to calculate the orientation of the elements of the snackbar is never set to the proper value.问题实际上是 android 本身的问题,从默认的 android dimen 文件加载的属性被破坏,因此用于计算小吃店元素方向的必需属性从未设置为正确的值。 The issue has been informed and the solution will be pushed to the source code soon.该问题已被告知,解决方案将尽快推送到源代码。

Workaround: set the attribute maxActionInlineWidth directly in your main theme and you can pickup the values from @dimen/design_snackbar_action_inline_max_width that is the one that should be used by android.解决方法:直接在您的主题中设置属性 maxActionInlineWidth,您可以从 @dimen/design_snackbar_action_inline_max_width 中获取值,这是 android 应该使用的值。

Example:例子:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="maxActionInlineWidth">@dimen/design_snackbar_action_inline_max_width</item>
</style>

If you go through the answer to THIS question, you can do something like below code for the action button(actually textview):如果您完成了这个问题的答案,您可以为操作按钮(实际上是 textview)执行以下代码:

var snackbar:Snackbar = Snackbar.make(root, message, Snackbar.LENGTH_SHORT)
val snackbarView = snackbar.view
val tv = snackbarView.findViewById<TextView>(android.support.design.R.id.snackbar_action)
tv.maxLines = 2
snackbar.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM