Android 百分比布局

开发工具 Android Studio 3.4.1

Android SDK 版本 29.0.1

1.第一步

打开app/build.gradle文件,在dependencies闭包中添加如下内容
Android 百分比布局_第1张图片
与《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 百分比布局_第2张图片
按照第二步第一块代码运行如下
Android 百分比布局_第3张图片

更多相关文章

  1. 后台动态添加布局文件、控件与动态设置属性
  2. Android开发之android_apk 在线安装(源代码分享)
  3. Android 在xml布局配置文件中给Button按钮添加事件
  4. vlc android 代码编译
  5. 关于Android多项目依赖在Eclipse中无法关联源代码的问题解决 (an
  6. Android 7.1 触摸事件代码跟踪
  7. android 开发中将十六进制 颜色代码 转换为int类型数值 方法 :

随机推荐

  1. Android如何通过https协议下载自己的http
  2. Android:手机扫描局域网所有ip,并进行sock
  3. Android中使用Gson解析JSON数据
  4. Cocos2d-x for android 使用Box2d报错解
  5. Android: 绘制的文本如何排版
  6. Android(安卓)ImageView图片自适应
  7. Android(安卓)Studio主要目录及文件简介
  8. 在Android模拟机上跑javaeye android 客
  9. android开发环境
  10. Android以太网框架情景分析之EthernetSer