在 Androidx 库上膨胀类 android.support.design.widget.FloatingActionButton 时出错

Gue*_*OCs 0 android android-layout android-studio

我在 Android 活动中添加了一个简单的小部件按钮:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:theme="@style/Theme.AppCompat"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="4sp"
    android:layout_marginTop="1sp"
    android:layout_marginRight="4sp"
    android:orientation="vertical">

    <ListView
        android:id="@+id/orders_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:focusable="auto" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:src="@drawable/ic_add"
        android:contentDescription="fazer pedido"
        android:layout_margin="16dp" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

然后我得到

android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class android.support.design.widget.FloatingActionButton
Run Code Online (Sandbox Code Playgroud)

这是我的 gradle 依赖项

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.squareup.okhttp3:okhttp:3.12.1'
    implementation 'org.json:json:20190722'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.10.1'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
Run Code Online (Sandbox Code Playgroud)

我在 stackoverflow 上尝试了所有解决方案。有些人提到使用backgroundTint,但我什至不使用它。其他一些人提到了 v7 紧凑库,但我正在使用 androidx 之一。所以这个问题看起来像是一个新问题,与其他问题不同

不知道是否有帮助,但这是我的活动:

public class OrdersActivity extends AppCompatActivity implements AbsListView.OnScrollListener{
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_orders);
Run Code Online (Sandbox Code Playgroud)

小智 6

您应该使用 Google 库中的浮动按钮。为此,您必须首先将库添加到依赖项文件中。这是代码行:

implementation "com.google.android.material:material:1.2.0-alpha3"
Run Code Online (Sandbox Code Playgroud)

然后,在您的 XML 文件中,替换:

 <android.support.design.widget.FloatingActionButton
Run Code Online (Sandbox Code Playgroud)

<com.google.android.material.floatingactionbutton.FloatingActionButton
Run Code Online (Sandbox Code Playgroud)