Andriod Studio版本:4.1.2
在按照《第一行代码:android(第二版)》学习百分比布局时,出现了无法导入百分比布局依赖库的问题,原因有以下几点:
1.AS已经不推荐使用compile导入依赖库
2.由于引入的第三方依赖库依赖了androidx ,而自己本身的库又是依赖的support ,导致两者不能共存。
解决方法:
1.将dependenci闭包修改如下
dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'androidx.percentlayout:percentlayout:1.0.0'
}
2.点击sync now,安装依赖库
3.将布局文件中的标签改为’androidx.percentlayout.widget.PercentFrameLayout’,例如:
<androidx.percentlayout.widget.PercentFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent">
<Button
android:id="@+id/button1"
android:text="Button 1"
android:layout_gravity="left|top"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"/>
</androidx.percentlayout.widget.PercentFrameLayout>
此时会出现如下图所示的错误,忽略即可
参考文章:
1.Android AndroidX迁移库
https://www.cnblogs.com/loaderman/p/12108359.html
2.Android Studio 控件的百分比布局
https://www.cnblogs.com/shi-win-snoopy/p/12128267.html
3.androidx 和 android support库不能共存
https://www.jianshu.com/p/4b723bb0a542?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation
另外,除了书中提到的四种布局外,郭霖老师也在博客上讲解了ConstraintLayout(约束性布局),我把链接放在这里,有兴趣的可以自行去学习
Android新特性介绍,ConstraintLayout完全解析
https://blog.csdn.net/guolin_blog/article/details/53122387