【问题标题】:Snackbar like google io 2018 android像 google io 2018 android 一样的 Snackbar
【发布时间】:2018-12-07 16:13:28
【问题描述】:

我怎样才能实现这样的小吃店。使其具有圆形边缘并显示在 fab 上方并且没有拉伸到屏幕的宽度

【问题讨论】:

    标签: android material-design android-snackbar material-components-android googleio


    【解决方案1】:

    Material Components library 中使用Snackbar
    默认情况下,它具有圆角。
    使用setAnchorView 方法使Snackbar 出现在特定视图上方,在您的情况下为fab 按钮。

            Snackbar snackbar = Snackbar.make(view, "...", Snackbar.LENGTH_LONG);
            snackbar.setAnchorView(fab);
            snackbar.show();
    

    您还可以使用应用主题中的 snackbarStyle 属性自定义边距。

      <style name="AppTheme" parent="Theme.MaterialComponents.Light">
        ....
        <item name="snackbarStyle">@style/MySnackbar</item>
      </style>
    

    在快餐栏样式中使用 android:layout_margin 属性。

      <style name="MySnackbar" parent="@style/Widget.MaterialComponents.Snackbar">
        <item name="android:layout_margin">32dp</item>
      </style>
    

    【讨论】:

    • 您指的是哪个版本的材料组件库。我正在使用最新的v1.1.0,但我的Snackbar 仍然没有圆角。
    【解决方案2】:

    这是 Google 最近推出的 Material Components 包的一部分。 可能在:-

    com.google.android.material:material:1.0.0-alpha3
    

    而它仍在 alpha 中。所以我不太确定你是否可以在生产中使用它。我在下面附上了一些链接。

    Material Component Snack Bar

    See all components list

    Samples

    【讨论】:

    • 目前还没有发布。团队已经好几天没有发布新版本了。
    【解决方案3】:

    尝试添加此依赖项。

    implementation 'com.github.danimahardhika:cafebar:1.3.1'
    

    然后可以使用此代码添加咖啡吧。

    CafeBar.builder(context)
    .theme(CafeBarTheme.LIGHT)
    .duration(CafeBar.Duration.MEDIUM)
    .content(R.string.text)
    .neutralText("Action")
    //You can parse string color
    .neutralColor(Color.parseColor("#EEFF41"))
    //Or use color resource
    .neutralColor(R.color.neutralText)
    .show();
    

    如果您需要更多自定义,请访问文档here

    【讨论】:

      猜你喜欢
      • 2019-01-31
      • 2021-04-15
      • 2011-06-02
      • 1970-01-01
      • 2017-10-03
      • 2019-09-27
      • 1970-01-01
      • 2016-01-13
      • 2011-10-05
      相关资源
      最近更新 更多