【问题标题】:Failed to find style 'floatingActionButtonStyle' in current theme在当前主题中找不到样式“floatingActionButtonStyle”
【发布时间】:2018-12-05 10:22:42
【问题描述】:

我是 android 开发的新手,所以我启动了 android studio “hello world” 附带的简单项目,但它给出了这个问题:

在当前主题中找不到样式“floatingActionButtonStyle” 我有 android studio 3.1.3 x86

build.gradle 文件中的我的 SDK 版本也是 28

这是 activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

这是我的 content_main.xml

        <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context=".MainActivity"
        tools:showIn="@layout/activity_main">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>

这是我的 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.admin.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".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>

</manifest>

【问题讨论】:

  • AS 有时会有一些滞后。您是否尝试过同步和重建您的项目?点击右侧窗格中的“Gradle”,然后按下蓝色的“同步”图标。然后,转到 Android Studio 顶部菜单上的 Build -> Rebuild Project。它应该会有所帮助。
  • 我试过了,但没有任何改变

标签: java android xml android-layout android-studio


【解决方案1】:

这可能是关于样式的“错误”错误:检查setOnClickListener 是否为遇到此问题的正确活动编写。也许您已经将此按钮的侦听器写入了错误的活动。您可以通过暂时评论setOnClickListener 的代码并重新启动构建来轻松测试它。

【讨论】:

    【解决方案2】:

    查看FloatingActionButton的来源可以看到构造函数已更改为

    public FloatingActionButton(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.floatingActionButtonStyle);
    }
    

    'com.android.support:appcompat-v7:28.0.0-alpha1' 中似乎默认不包含新的floatingActionButtonStyle

    我发现的最佳解决方法(基于类似情况here)是在您的主题中定义属性(可能仅在调试版本中),这样错误就消失了,因为@style/Widget.Design.FloatingActionButton 已经在支持库中定义所以您不需要创建样式,只需在 AppTheme 文件中的 AppTheme 样式中引用它即可。

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="floatingActionButtonStyle">@style/Widget.Design.FloatingActionButton</item>
    </style>
    

    确保在布局预览和干净构建中使用 AppTheme。

    【讨论】:

      猜你喜欢
      • 2018-08-23
      • 2011-11-14
      • 1970-01-01
      • 1970-01-01
      • 2018-08-15
      • 2012-01-20
      • 2019-01-31
      • 1970-01-01
      相关资源
      最近更新 更多