Android开发学习教程(16)- Android布局之百分比布局PercentLayout

0 652

—— 世之奇伟、瑰怪、非常之观,常在于险远,而人之所罕至焉,故非有志者不能至也。

上一篇我们讲了帧布局FrameLayout的基本用法,这里来学习常用布局之百分比布局PercentLayout的基本用法。

百分比布局是什么

百分比布局中顾名思义就是按照百分比固定控件的长或宽。百分比布局大致可分为三种,一种是我们上两篇已经学过的线性布局LinearLayout,它可以通过设置子控件的layout_weight属性来实现百分比效果;第二三种是谷歌提供的支持库PercentFrameLayout和PercentRelativeLayout;

百分比布局有什么用

按照百分比固定控件的长或宽,对适配不同分辨率手机有一定帮助。

百分布局怎么用

第一种百分比布局LinearLayout

继续基于上一篇的项目,我们新建一个LinearLayout:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="utf-8"?>
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:background="@android:color/holo_red_light" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="2"
        android:background="@android:color/holo_green_light" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="3"
        android:background="@android:color/holo_blue_bright" />
</LinearLayout>

Android开发学习教程(16)- Android布局之百分比布局PercentLayout

首先布局是线性布局LinearLayout,属性android:orientation="horizontal",则所有子控件横向排列。第一个子控件android:layout_width="0dp"、android:layout_weight="1"则是控件的宽度不固定,占父控件LinearLayout的1/6;第二个子控件android:layout_width="0dp"、android:layout_weight="2"则是控件的宽度不固定,占父控件LinearLayout的2/6;第三个子控件android:layout_width="0dp"、android:layout_weight="3"则是控件的宽度不固定,占父控件LinearLayout的3/6;

没有设置任何属性,控件默认显示在FrameLayout左上角

PercentFramLayout是谷歌提供的支持库,使用之前需在项目的build.gradle文件引入支持库

1
implementation "androidx.percentlayout:percentlayout:1.0.0"

然后我们新建一个PercentFrameLayout:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?xml version="1.0" encoding="utf-8"?>
<androidx.percentlayout.widget.PercentFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_red_light"
        app:layout_heightPercent="10%"
        app:layout_widthPercent="10%" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_green_light"
        app:layout_heightPercent="20%"
        app:layout_marginLeftPercent="10%"
        app:layout_widthPercent="20%" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_bright"
        app:layout_heightPercent="30%"
        app:layout_marginLeftPercent="30%"
        app:layout_widthPercent="30%" />
</androidx.percentlayout.widget.PercentFrameLayout>

Android开发学习教程(16)- Android布局之百分比布局PercentLayout

首先布局是PercentFrameLayout,顾名思义是一个具有百分比属性的FrameLayout。因为布局FrameLayout不能像LinearLayout的子控件一样通过layout_weight来设置子控件的百分比,所以谷歌提供了PercentFrameLayout布局来实现基于FrameLayout的百分比布局。当布局设置为PercentFrameLayout的时候,子控件有如下属性:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
app:layout_heightPercent:用百分比表示高度
app:layout_widthPercent:用百分比表示宽度
app:layout_marginPercent:用百分比表示View之间的间隔
app:layout_marginLeftPercent:用百分比表示左边间隔
app:layout_marginRight:用百分比表示右边间隔
app:layout_marginTopPercent:用百分比表示顶部间隔
app:layout_marginBottomPercent:用百分比表示底部间隔
app:layout_marginStartPercent:用百分比表示距离第一个View之间的距离
app:layout_marginEndPercent:用百分比表示距离最后一个View之间的距离
app:layout_aspectRatio:用百分比表示View的宽高比

先来看第一个子控件

1
2
3
4
5
6
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_red_light"
    app:layout_heightPercent="10%"
    app:layout_widthPercent="10%" />

表示控件TextView的高度占PercentFrameLayout高度的10%,宽度占PercentFrameLayout宽度的10%

第二个子控件

1
2
3
4
5
6
7
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_green_light"
    app:layout_heightPercent="20%"
    app:layout_marginLeftPercent="10%"
    app:layout_widthPercent="20%" />

表示控件TextView的高度占PercentFrameLayout高度的20%,宽度占PercentFrameLayout宽度的20%,距离PercentFrameLayout左边的距离是PercentFrameLayout宽度的10%,同理,第三个控件TextView的高度占PercentFrameLayout高度的30%,宽度占PercentFrameLayout宽度的30%,距离PercentFrameLayout左边的距离是PercentFrameLayout宽度的30%。

第三种百分比布局PercentRelativeLayout,我们再来用布局PercentRelativeLayout 实现上面的效果,因为PercentRelativeLayout也是继承于RelativeLayout。所以结合PercentRelativeLayout的属性和RelativeLayout属性其实也很简单,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?xml version="1.0" encoding="utf-8"?>
<androidx.percentlayout.widget.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_red_light"
        app:layout_heightPercent="10%"
        app:layout_widthPercent="10%" />
    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/tv1"
        android:background="@android:color/holo_green_light"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="20%" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/tv2"
        android:background="@android:color/holo_blue_bright"
        app:layout_heightPercent="30%"
        app:layout_widthPercent="30%" />
</androidx.percentlayout.widget.PercentRelativeLayout>
收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章资源,如无特殊说明或标注,均为本站网友和创作者贡献分享。如若本站内容侵犯了原著者的合法权益,可联系网站客服QQ2743319061删除。

云炬星球 安卓教程 Android开发学习教程(16)- Android布局之百分比布局PercentLayout https://src.yunjunet.cn/876768.html

常见问题
  • 放心亲,我们不会为了几十块钱的东西坏了名声!
查看详情
  • 方法一:点击“立即下载.”按钮,付款后在下载弹窗的虚线框的隐藏信息里获取 方法二:在正文底部使用VIP查看隐藏的解压密码 方法三:联系【云炬网络】公众号客服获取
查看详情
  • 付款后会出现“立即下载”按钮(点击即可下载),如果下载失败也可以联系客服发订单截图补发。
查看详情
  • 登录购买会多端同步购买记录,永久可以查看反复下载;非登录购买仅将购买记录保存到本地浏览器中,浏览器cookie清除后无法再次下载。先右上角点登录,然后点击微信图标可以快速授权注册登录^_^
查看详情
  • 可以试看。点击”查看演示“或“试看预览”按钮可以试读从资料目录中节选的部分内容,也可以自己指定想试看的内容。
查看详情
  • 原因一:本站所有资源已开启有效性检测(服务器24h全自动监测),当监测到下载链接无法访问时会提示“该资源已失效,请勿购买”,遇到这种情况可以联系客服修复失效的下载链接,或直接联系客服在淘宝下单购买即可。(检测原理:购买前服务器程序会预访问下载链接,响应值为200说明资源有效允许购买,响应值为404或502等报错说明资源失效禁止购买)。原因二:上传者未启用“下载”选项。
查看详情
官方客服团队

为您解决烦忧 - 24小时在线 专业服务