《第一行代码》百分比布局

问题:

依照书中P106关于百分比布局的写法,我遇到的问题如下:

1. ERROR: Failed to resolve: android.support:percent:24.2.1

2.  Android resource linking failed. ···Attribute android:layout_heightPercent not found.

注:我的android studio版本是3.4.1

解决方法:

步骤一:首先打开app/build.gradle文件,在dependencies中添加百分比布局依赖包implementation 'androidx.percentlayout:percentlayout:1.0.0'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
    //添加该行
    implementation 'androidx.percentlayout:percentlayout:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.3.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

步骤二:打开activity_main.xml文件,删除该文件里所有代码,复制粘贴下面的代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.percentlayout.widget.PercentFrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:layout_gravity="left|top"
        android:text="东"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_heightPercent="50%"
        app:layout_widthPercent="50%" />

    <Button
        android:id="@+id/button2"
        android:layout_gravity="right|top"
        android:text="西"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_heightPercent="50%"
        app:layout_widthPercent="50%" />

    <Button
        android:id="@+id/button3"
        android:layout_gravity="left|bottom"
        android:text=" 南"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_heightPercent="50%"
        app:layout_widthPercent="50%" />

    <Button
        android:id="@+id/button4"
        android:layout_gravity="right|bottom"
        android:text="北"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_heightPercent="50%"
        app:layout_widthPercent="50%" />

</androidx.percentlayout.widget.PercentFrameLayout>

步骤三:run

展示效果

 

感谢博客1的指导,写的非常细致!各位可作参考 

 
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值