在当前主题中找不到样式“ floatingActionButtonStyle”

时间:2018-06-26 16:08:17

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

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

无法在当前主题中找到样式'floatingActionButtonStyle' 我有android studio 3.1.3 x86

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

这是 activity_main.xml

0.0

这是我的 content_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>

这是我的 AndroidManifest.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>

2 个答案:

答案 0 :(得分:5)

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

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

有一个新的floatingActionButtonStyle,似乎没有默认包含在'com.android.support:appcompat-v7:28.0.0-alpha1'

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

<!-- 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并清理构建。

答案 1 :(得分:1)

这可能是有关样式的“错误”错误:检查setOnClickListener是否为发生此问题的正确活动写了字。也许您已将此按钮的侦听器编写为错误的活动。您可以通过临时注释有关setOnClickListener的代码并重新启动该版本来轻松对其进行测试。