【问题标题】:How to use a Snackbar with AndroidX?如何在 AndroidX 中使用 Snackbar?
【发布时间】:2019-03-11 02:56:28
【问题描述】:

我正在尝试将Snackbar 添加到我的应用中,

Snackbar.make(findViewById(android.R.id.content), 
            R.string.not_found, Snackbar.LENGTH_INDEFINITE).show();

但遇到此错误消息:

10-05 16:48:24.530 28492-28492/nl.minerall.sapphire.pocket.full E/AndroidRuntime: FATAL EXCEPTION: main
    Process: nl.minerall.sapphire.pocket.full, PID: 28492
    android.view.InflateException: Binary XML file line #41: Error inflating class <unknown>
        at android.view.LayoutInflater.createView(LayoutInflater.java:633)
        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at com.google.android.material.snackbar.Snackbar.make(Snackbar.java:188)
        at com.google.android.material.snackbar.Snackbar.make(Snackbar.java:230)

我用谷歌搜索了一些,它告诉我必须将com.android.support:design 添加到我的build.gradle,但这完全破坏了我的构建,因为我使用的是 AndroidX:

dependencies {
    implementation 'androidx.appcompat:appcompat:1.0.0'   
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'com.google.android:flexbox:1.1.0'

    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.legacy:legacy-support-v13:1.0.0'

    implementation 'androidx.coordinatorlayout:coordinatorlayout:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
}

更新我翻了一下,发现这个 XML 文件正在膨胀 (/extras/android/support/design/res/layout/design_layout_snackbar_include.xml):

<?xml version="1.0" encoding="utf-8"?>
<!--
  ~ Copyright (C) 2015 The Android Open Source Project
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~      http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
-->

<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
            android:id="@+id/snackbar_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingTop="@dimen/design_snackbar_padding_vertical"
            android:paddingBottom="@dimen/design_snackbar_padding_vertical"
            android:paddingLeft="@dimen/design_snackbar_padding_horizontal"
            android:paddingRight="@dimen/design_snackbar_padding_horizontal"
            android:textAppearance="@style/TextAppearance.Design.Snackbar.Message"
            android:maxLines="@integer/design_snackbar_text_max_lines"
            android:layout_gravity="center_vertical|left|start"
            android:ellipsize="end"
            android:textAlignment="viewStart"/>

    <Button
            android:id="@+id/snackbar_action"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/design_snackbar_extra_spacing_horizontal"
            android:layout_marginStart="@dimen/design_snackbar_extra_spacing_horizontal"
            android:layout_gravity="center_vertical|right|end"
            android:paddingTop="@dimen/design_snackbar_padding_vertical"
            android:paddingBottom="@dimen/design_snackbar_padding_vertical"
            android:paddingLeft="@dimen/design_snackbar_padding_horizontal"
            android:paddingRight="@dimen/design_snackbar_padding_horizontal"
            android:visibility="gone"
            android:textColor="?attr/colorAccent"
            style="?attr/borderlessButtonStyle"/>

</merge>

当只是将该文件复制到我的项目并在 Android Studio 中打开它时,?attr/colorAccent 似乎有问题,因为当我删除它时,设计器没有显示任何错误。但是,如何解决这一切...?

【问题讨论】:

  • 到目前为止我还没有使用过snacker,但我最近开始使用androidx,我通过从sunflower示例应用程序复制解决了我所有的依赖问题。因为在线经常导入支持库,您正确地说它不会像androidx那样工作。所以看一下,这个片段使用了snackbar。 gradle 导入通常在 build.gradle 中:github.com/googlesamples/android-sunflower/blob/master/app/src/…
  • 实际读取你的异常,你确定是导入的问题吗?你可以发布你的xml文件吗?因为异常确实正确引用了 android 材质的snackbar。我认为问题出在 xml 文件的第 41 行。
  • @findusl 没有 XML。我只想能够在任何布局上使用Snackbar 而不是Toast。我尝试使用 android.R.id.content 作为通用 ID。
  • @findusl 我来看看那个向日葵代码。
  • 不知道对你有没有帮助,不过这个好像是xml膨胀失败:github.com/material-components/material-components-android/blob/…

标签: android-snackbar androidx


【解决方案1】:

你需要导入

com.google.android.material.snackbar.Snackbar

而不是

android.support.design.widget.Snackbar

【讨论】:

    【解决方案2】:

    我添加到我的 build.gradle(Module: app)

    implementation 'com.android.support:design:28.0.0'
    

    它工作正常,我的 gradle 构建成功。

    这些是我的依赖项:-

    dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android:flexbox:1.1.0'
    implementation 'androidx.coordinatorlayout:coordinatorlayout:1.0.0'
    implementation 'com.android.support:design:28.0.0'
    

    }

    【讨论】:

    • 这使得项目不再构建。据我了解,AndroidX 正在替换支持库。
    【解决方案3】:

    你应该使用的是这个:

    implementation "com.google.android.material:material:1.1.0-alpha06"

    如果您知道如何以支持方式配置依赖项,可以查看Migrating to AndroidX 上的 Android 开发者页面。 另外,请检查最新版本,在您阅读本文时,它可能不是 1.1.0-alpha06

    【讨论】:

    • @Bart Friederichs 如果这个答案对您有帮助,或者根据您的问题是正确的,请考虑接受它。
    猜你喜欢
    • 2019-03-06
    • 1970-01-01
    • 2020-02-25
    • 2021-03-16
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    相关资源
    最近更新 更多