赞
踩
与《第一行代码Android》中作者所述内容不同,AS 3.5.1的build.gradle内部是这样的
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
可以看到和书上的内容不同,通过查阅android developers,得知在java上的percent类已经被弃用了,官方给出了另一种做法,先贴代码
<androidx.constraintlayout.widget.ConstraintLayout 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"> <androidx.constraintlayout.widget.Guideline android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/left_guideline" app:layout_constraintGuide_percent=".15" android:orientation="vertical"/> <androidx.constraintlayout.widget.Guideline android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/right_guideline" app:layout_constraintGuide_percent=".85" android:orientation="vertical"/> <androidx.constraintlayout.widget.Guideline android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/top_guideline" app:layout_constraintGuide_percent=".15" android:orientation="horizontal"/> <androidx.constraintlayout.widget.Guideline android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/bottom_guideline" app:layout_constraintGuide_percent=".85" android:orientation="horizontal"/> <Button android:text="Button" android:layout_width="0dp" android:layout_height="0dp" android:id="@+id/button" app:layout_constraintLeft_toLeftOf="@+id/left_guideline" app:layout_constraintRight_toRightOf="@+id/right_guideline" app:layout_constraintTop_toTopOf="@+id/top_guideline" app:layout_constraintBottom_toBottomOf="@+id/bottom_guideline" /> </androidx.constraintlayout.widget.ConstraintLayout>
本人作为初学者已经懵逼了,对于上述代码官方给出的说法是:
consider using ConstraintLayout and associated layouts instead
考虑改用ConstraintLayout和关联的布局。
所以有能力的可以考虑上述做法用java实现百分比布局
Android Developers网站:https://developer.android.google.cn/?hl=en
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。