Android屏幕适配之百分比布局LinearLayout、RelativeLayout、FrameLayout

本文介绍谷歌已废弃的百分比布局库,并转向推荐使用约束布局。曾广泛使用的AndroidPercentSupportLibrary现已过时,文章回顾其使用方法,包括依赖添加、自定义属性及示例代码。同时,提及第三方库PercentLinearLayout的引入与应用案例。
部署运行你感兴趣的模型镜像

谷歌的百分比库已废弃,转用约束布局,本博文仅供参考思路。新方法请参考:https://blog.youkuaiyun.com/qq_21480607/article/details/98790567

一、首先说一下Google官方提供的百分比布局兼容库:

https://developer.android.google.cn/reference/android/support/percent/package-summary?hl=en#interfaces
Android Percent Support Library,目前它支持RelativeLayout和FrameLayout的百分比布局。
使用前添加依赖,定义在support中

implementation 'com.android.support:percent:xx.x.x'

目前最新版本为:implementation ‘com.android.support:percent:28.0.0’
查看网址:https://developer.android.google.cn/topic/libraries/support-library/revisions.html?hl=en
关于compile、implementation与api的区别请查看:https://blog.youkuaiyun.com/qq_21480607/article/details/98477660
由于百分比布局不是内置在系统SDK当中,所以要把完整的包路径写出来。同时要定义一个app的命名空间,这样才能使用百分比布局的自定义属性。

xmlns:app:"http://schemas.android.com/apk/res-auto”

之后就可以使用app:layout_widthPercent和layout_heightPercent属性了。

主要有以下属性
  • 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的宽高比
相关案例:

好几个

<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
<TextView
    android:layout_alignParentBottom="true"
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#87CEFA"
    app:layout_heightPercent="100%"
    app:layout_marginPercent="4%"
    app:layout_widthPercent="10%"
    android:text="100%"
    android:gravity="center"
    />

<TextView
    android:layout_alignParentBottom="true"
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/textView"
    android:background="#87CEFA"
    app:layout_heightPercent="80%"
    app:layout_marginPercent="4%"
    app:layout_widthPercent="10%"
    android:text="80%"
    android:gravity="center"
    />

<TextView
    android:layout_alignParentBottom="true"
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/textView1"
    android:background="#87CEFA"
    app:layout_heightPercent="60%"
    app:layout_marginPercent="4%"
    app:layout_widthPercent="10%"
    android:text="60%"
    android:gravity="center"
    />

<TextView
    android:layout_alignParentBottom="true"
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/textView2"
    android:background="#87CEFA"
    app:layout_heightPercent="40%"
    app:layout_marginPercent="4%"
    app:layout_widthPercent="10%"
    android:text="40%"
    android:gravity="center"
    />

<TextView
    android:layout_alignParentBottom="true"
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/textView3"
    android:background="#87CEFA"
    app:layout_heightPercent="20%"
    app:layout_marginPercent="4%"
    app:layout_widthPercent="10%"
    android:text="20%"
    android:gravity="center"
    />
</android.support.percent.PercentRelativeLayout>

二、有大牛改写了官方库 添加了PercentLinearLayout

对于如何导入,也是相当的简单,android studio的用户,直接:

dependencies {
    //...
    compile 'com.zhy:percent-support-extends:1.0.1'
}

不需要导入官方的percent-support-lib了。

三个类分别为:

com.zhy.android.percent.support.PercentLinearLayout
com.zhy.android.percent.support.PercentRelativeLayout
com.zhy.android.percent.support.PercentFrameLayout

使用案例:
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<com.zhy.android.percent.support.PercentLinearLayout
    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="wrap_content"
    android:orientation="vertical">
<com.zhy.android.percent.support.PercentFrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF4082">
  <ImageView
      android:layout_width="0dp"
      android:layout_height="0dp"
      android:src="@drawable/actbar_home_up_indicator"
      app:layout_heightPercent="6%"
      app:layout_widthPercent="15%"/>
    <TextView
        android:layout_alignParentTop="true"
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/white"
        app:layout_marginTopPercent="1%"
        app:layout_marginBottomPercent="1%"
        app:layout_heightPercent="4%"
        app:layout_widthPercent="100%"
        android:autoSizeMaxTextSize="80dp"
        android:autoSizeMinTextSize="16dp"
        android:autoSizeTextType="uniform"
        android:text="我的好友"
        android:textSize="26dp"
        android:gravity="center"
        />
    <TextView
        android:layout_alignParentTop="true"
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/white"
        app:layout_marginTopPercent="2%"
        app:layout_marginBottomPercent="1%"
        app:layout_marginLeftPercent="80%"
        app:layout_heightPercent="3%"
        app:layout_widthPercent="100%"
        android:autoSizeMaxTextSize="80dp"
        android:autoSizeMinTextSize="16dp"
        android:autoSizeTextType="uniform"
        android:text="添加好友"
        android:textSize="26dp"
        />
  </com.zhy.android.percent.support.PercentFrameLayout>
</com.zhy.android.percent.support.PercentLinearLayout>

详细介绍请见博主博文:https://blog.youkuaiyun.com/lmj623565791/article/details/46767825

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

隔壁de小刘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值