Android Studio 3.5.1 在项目的build.gradle中添加百分比布局库
16lz
2021-01-23
与《第一行代码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
更多相关文章
- Android 代码设置来电铃声
- Android自定义Dialog(自定义主题、自定义布局)
- Android 的整体布局
- android 执行shell命令代码
- android 开发中常用颜色,以及一些颜色的代码
- Android Camera子系统之源代码View
- android 五种 布局文件
- Android开发之常用代码片段