Android Studio 3.5.2 使用FloatingActionButton

背景:

最近在写安卓大作业,需要引入FloatingActionButton控件,按照《第一行代码》第二版 中的步骤去做,无法成功引入。

编程环境:

IDE版本: Android Studio 3.4.2

gradle版本:5.4.1

问题现象:

按照书本修改build.gradle文件,在dependencies部分添加
implementation ‘com.android.support:design:28.0.0’后,重新build,然后打开java源程序,准备在其中导入空件。但在布局文件中无法导入FloatingActionButton。
在这里插入图片描述
布局文件代码

<android.support.design.widget.FloatingActionButton
        android:id="@+id/main_fab_enter_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="36dp"
        android:layout_marginLeft="30dp"
        android:layout_marginStart="30dp"
        android:src="@drawable/add"
        app:backgroundTint="#ee722f"
        app:borderWidth="0dp"
        app:elevation="6dp"
        app:fabSize="normal"
        app:layout_anchorGravity="bottom|right"
        app:pressedTranslationZ="12dp"
        app:rippleColor="#a6a6a6"/>

于是开启了我疯狂检索解决办法之旅。

问题原因:

由于google准备将Support Library 迁移到androidX,所以按照书本的方法添加依赖,是无法导入的。目前google已将Support Library迁移至 ‘com.google.android.material:material:1.0.0’,

解决方式:

如下所示添加依赖:

ext {
    appCompatVersion = '1.0.0'
    designSupportVersion = '1.0.0'
    recyclerViewVersion = '1.0.0'
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.github.luweibin3118:PieChartView:v1.1.2'
    implementation 'com.jakewharton:butterknife:7.0.1'
    implementation 'cc.trity.floatingactionbutton:library:1.0.0'
    implementation 'org.greenrobot:eventbus:3.0.0'
    testImplementation 'junit:junit:4.12'
    debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.1'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation "androidx.appcompat:appcompat:${appCompatVersion}"
    implementation "com.google.android.material:material:${designSupportVersion}"//添加本行,最重要
    implementation "androidx.recyclerview:recyclerview:${recyclerViewVersion}"
    implementation "androidx.percentlayout:percentlayout:1.0.0"
    implementation "androidx.vectordrawable:vectordrawable:1.0.0" // VectorDrawableCompat
    implementation "androidx.vectordrawable:vectordrawable-animated:1.0.0" // AnimatedVectorDrawableCompat
    implementation "androidx.transition:transition:1.1.0"
    implementation "androidx.annotation:annotation:1.1.0"
}

添加完成后点击syno now,然后在java源文件中输入

import com.google.android.material.floatingactionbutton.FloatingActionButton; //导入FloatingActionButton控件

并修改布局文件代码

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/main_fab_enter_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="36dp"
        android:layout_marginLeft="30dp"
        android:layout_marginStart="30dp"
        android:src="@drawable/add"
        app:backgroundTint="#ee722f"
        app:borderWidth="0dp"
        app:elevation="6dp"
        app:fabSize="normal"
        app:layout_anchorGravity="bottom|right"
        app:pressedTranslationZ="12dp"
        app:rippleColor="#a6a6a6"/>

至此,控件成功导入。
在这里插入图片描述

后记:

搜寻了好多资料,最后是因为没有在布局文件中写好正确的引入语句,导致一直无法引入,哭了。
还有一点就是,遇到问题不要慌,相信自己一定能够解决它。

谢谢!!!!!

参考博客

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值