【问题标题】:How to use Snackbar without support library?如何在没有支持库的情况下使用 Snackbar?
【发布时间】:2016-04-19 10:11:42
【问题描述】:

我正在开发一个不需要向后兼容的 Android 应用。截至目前,目标 SDK 版本为 22。我正在使用本机 Activity 和 Fragment,应用程序主题是 android:Theme.Material.Light。 我的问题是我无法在现有设置中使用 Snackbar,它会抛出异常,例如

android.view.InflateException: Binary XML file line #18: Error inflating class android.support.design.widget.Snackbar$SnackbarLayout E/AndroidRuntime(19107): at android.view.LayoutInflater.createView(LayoutInflater.java: 640) E/AndroidRuntime(19107): 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)

我用谷歌搜索,但找不到任何带有 Activity 的小吃店示例。那么是否有必要使用像

这样的支持库

AppCompatActivity 或 android.support.v4.app.Fragment。

为了在我的应用中使用 Snackbar?

【问题讨论】:

    标签: android android-fragments android-support-library android-snackbar


    【解决方案1】:

    您需要使用支持设计库compile 'com.android.support:design:23.0.1' 才能工作:

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:23.0.1'
        compile 'com.android.support:design:23.0.1'
    }
    

    (Read more in detail here)

    【讨论】:

    • 但它会再次要求我更改主题,然后是 AppCompatActivity 等等。这就是我的问题。随着您的更改,我再次收到异常“java.lang.IllegalArgumentException:您需要将 Theme.AppCompat 主题(或后代)与设计库一起使用。”
    • SnackBar 包含在该库中,因此如果要使用该功能,则需要添加依赖项。并用所有必需的约束来满足库。否则,您可以尝试一个模仿 Snackbar 行为的外部库。但是,这有其自身的局限性,不会提供像原始 SnackBar 那样的体验。
    • 这意味着为了使用 Snackbar,我必须使用支持库(AppCompatActivity 等),并且 'com.android.support:design:23.0.1' 与标准 Material 主题不兼容。
    • 是的,它甚至适用于 Activity 和 Fragment,只需要 gradle 和主题更改。
    • @Amit,我必须更改主题吗?
    【解决方案2】:

    是的,在你的 gradle 上添加依赖项

    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.android.support:design:23.0.1'
    

    然后,相应地更改您的应用主题,您需要使用AppCompat 主题。在您的 Styles.xml 上创建以下主题

    <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>
    </style>
    
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    

    然后在您的清单中:在应用程序上添加 @style/AppTheme 并在每个活动上添加 @style/AppTheme.NoActionBar

    <application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
    
        <activity
            android:name=".Activities.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
         </activity>
    </application>
    

    【讨论】:

      【解决方案3】:

      如果您像我一样固执,不想使用支持库,但想在您的应用程序中使用小吃栏,那么您可以选择。我发现this deprecated library 本质上就是你所知道的 Snackbar,只是独立于支持库。它对我来说很好用,但是它可能没有一些功能,例如在出现时向上移动 FloatingActionButton。

      【讨论】:

      • 绝妙的提示。非常好 - 为我节省了第三次尝试使用“AppCompat”蠕虫的失败尝试。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-06
      • 2015-07-21
      • 2021-01-31
      • 1970-01-01
      • 2019-12-07
      • 2021-01-25
      相关资源
      最近更新 更多