Android 百分比布局
16lz
2021-01-23
Android 百分比布局
开发工具 Android Studio 3.4.1
Android SDK 版本 29.0.1
1.第一步
打开app/build.gradle文件,在dependencies闭包中添加如下内容
与《Android 第一行代码》不同的是,书上添加的是:
compile 'com.android.support:percent:24.2.1'
而系统会提醒你将’compile’替换为’implementation’或者’api’,最后的版本号是你的SDK版本
添加完之后会出现下图所示,点击Sync Now同步即可
2.第二步
修改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/button_1" android:layout_gravity="left|top" app:layout_widthPercent="50%" app:layout_heightPercent="50%" android:text="Buton1" android:textIsSelectable="true"/> <Button android:id="@+id/button_2" android:layout_gravity="right|top" app:layout_widthPercent="50%" app:layout_heightPercent="50%" android:text="Button2" android:textIsSelectable="true"/> <Button android:id="@+id/button_3" android:layout_gravity="left|bottom" android:text="Button3" android:textIsSelectable="true" app:layout_widthPercent="50%" app:layout_heightPercent="50%"/> <Button android:id="@+id/button_4" android:layout_gravity="right|bottom" android:text="Button4" android:textIsSelectable="true" app:layout_widthPercent="50%" app:layout_heightPercent="50%" />androidx.percentlayout.widget.PercentFrameLayout>
《Android 第一行代码》中的修改如下,注意与上方代码块不同之处是头和尾
<?xml version="1.0" encoding="utf-8"?><android.support.percent.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/button_1" android:layout_gravity="left|top" app:layout_widthPercent="50%" app:layout_heightPercent="50%" android:text="Buton1" android:textIsSelectable="true"/> <Button android:id="@+id/button_2" android:layout_gravity="right|top" app:layout_widthPercent="50%" app:layout_heightPercent="50%" android:text="Button2" android:textIsSelectable="true"/> <Button android:id="@+id/button_3" android:layout_gravity="left|bottom" android:text="Button3" android:textIsSelectable="true" app:layout_widthPercent="50%" app:layout_heightPercent="50%"/> <Button android:id="@+id/button_4" android:layout_gravity="right|bottom" android:text="Button4" android:textIsSelectable="true" app:layout_widthPercent="50%" app:layout_heightPercent="50%" />android.support.percent.PercentFrameLayout>
当完成第一步,再按照书上所讲修改xml中的代码时,会出现如下图所示问题
按照第二步第一块代码运行如下
更多相关文章
- 后台动态添加布局文件、控件与动态设置属性
- Android开发之android_apk 在线安装(源代码分享)
- Android 在xml布局配置文件中给Button按钮添加事件
- vlc android 代码编译
- 关于Android多项目依赖在Eclipse中无法关联源代码的问题解决 (an
- Android 7.1 触摸事件代码跟踪
- android 开发中将十六进制 颜色代码 转换为int类型数值 方法 :