问题:
依照书中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的指导,写的非常细致!各位可作参考