最近在写安卓大作业,需要引入FloatingActionButton控件,按照《第一行代码》第二版 中的步骤去做,无法成功引入。
编程环境:gradle版本:5.4.1
问题现象:按照书本修改build.gradle文件,在dependencies部分添加
implementation ‘com.android.support:design:28.0.0’后,重新build,然后打开java源程序,准备在其中导入空件。但在布局文件中无法导入FloatingActionButton。
布局文件代码
于是开启了我疯狂检索解决办法之旅。
问题原因:由于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控件
并修改布局文件代码
至此,控件成功导入。
搜寻了好多资料,最后是因为没有在布局文件中写好正确的引入语句,导致一直无法引入,哭了。
还有一点就是,遇到问题不要慌,相信自己一定能够解决它。
谢谢!!!!!
参考博客



