无法导入‘com.android.support.percent‘(百分比布局依赖库)的解决方法

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

  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值