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中的代码时,会出现如下图所示问题

按照第二步第一块代码运行如下

更多相关文章

  1. 后台动态添加布局文件、控件与动态设置属性
  2. android framework 层服务 分析
  3. Android(安卓)支持的 media 文件格式--MediaFile
  4. Android(安卓)在xml布局配置文件中给Button按钮添加事件
  5. Eclipse+CDT+GDB调试android NDK程序 轉
  6. vlc android 代码编译
  7. Android(安卓)Pull解析xml
  8. Android开发之android_apk 在线安装(源代码分享)
  9. android 5.1 添加reboot 飞行模式 silent

随机推荐

  1. mysql导出查询结果到csv的实现方法
  2. Windows平台下MySQL安装与配置方法与注意
  3. mysql 使用inet_aton和inet_ntoa处理ip地
  4. mysql 查看当前使用的配置文件my.cnf的方
  5. deepin 2014系统下安装mysql数据库的方法
  6. mysql 搜寻附近N公里内数据的简单实例
  7. MySQL通过触发器解决数据库中表的行数限
  8. 浅谈mysql explain中key_len的计算方法
  9. mysql 转换NULL数据方法(必看)
  10. 解决修改mysql的data_dir所引发的错误问