转载自http://www.apkbus.com/forum.php?mod=viewthread&tid=244752&extra=&_dsign=0b699c42

在此之前,相信大家都已经对Android API所提供的布局方式非常熟悉了。也许在接触Android的时候都有过这样的想法,如果可以按照百分比的方式进行界面布局,这样适配各种屏幕就简单多了吧!!以前的一个小梦想,现在终于得以实现,谷歌正式提供百分比布局支持库(percent-support-lib)。
<ignore_js_op>



获取支持库:

使用Android studio在build.gradle添加以下信息就可以获取支持库,当然了,如果你没有下载到该支持库会提示你下载。
[AppleScript] 纯文本查看 复制代码
1
2
3
4
5
dependencies {
 
    compile 'com.android.support:percent:22.2.0'
 
}



新的布局组件:

在这个包里面有两个新的容器类
1、PercentRelativeLayout
<ignore_js_op>
2、PercentFrameLayout
<ignore_js_op>
在此看来,这两个类很显然是继承自 FrameLayout 和 RelativeLayout 两个容器类。



新的属性设置:

新的容器有了一些设置百分比的属性,下面我们来了解一下:
  • layout_widthPercent
设置控件宽度为父容器的宽的百分比
  • layout_heightPercent
设置控件高度为父容器的高的百分比
  • layout_marginPercent

  • layout_marginLeftPercent
设置控件与左边控件的距离为父容器的宽度的百分比
  • layout_marginTopPercent
设置控件与上方控件的距离为父容器的高度的百分比
  • layout_marginRightPercent
设置控件与右边控件的距离为父容器的宽度的百分比
  • layout_marginBottomPercent
设置控件与下方控件的距离为父容器的高度的百分比
  • layout_marginStartPercent
与上面的说明类似
  • layout_marginEndPercent
与上面的说明类似
从命名的方式我们可以知道,原来用某些具体单位(如dp)的设置现在都可以用百分比的方式进行设置了,例如设置控件的宽度layout_width原来我们是这样玩的android:layout_width="match_parent"现在用了百分比的属性之后呢,可以这样玩了app:layout_widthPercent="50%",这里的百分比是相对于父容器而言的。



使用介绍:

官方文档地址:https://juliengenoud.github.io/android-percent-support-lib-sample/
在布局的xml文件需要添加下面这行来声明
[XML] 纯文本查看 复制代码
官方示例代码:
[XML] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
<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"/>
 
     <ImageView
 
         app:layout_widthPercent="50%"
 
         app:layout_heightPercent="50%"
 
         app:layout_marginTopPercent="25%"
 
         app:layout_marginLeftPercent="25%"/>
 
 </android.support.percent.PercentFrameLayout/>
上面是官方文档的使用示例代码,但是实际上使用的时候如果没设置控件的宽高是会报错的。
实验的布局代码:
[XML] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<android.support.percent.PercentRelativeLayout
 
 
 
    android:layout_width="match_parent"
 
    android:layout_height="match_parent">
 
  
 
    <View
 
        android:id="@+id/top_left"
 
        android:layout_width="0dp"
 
        android:layout_height="0dp"
 
        android:layout_alignParentTop="true"
 
        android:background="#ff0000"
 
        app:layout_heightPercent="30%"
 
        app:layout_widthPercent="70%" />
 
  
 
    <View
 
        android:id="@+id/top_right"
 
        android:layout_width="0dp"
 
        android:layout_height="0dp"
 
        android:layout_alignParentTop="true"
 
        android:layout_toRightOf="@+id/top_left"
 
        android:background="#00ff00"
 
        app:layout_heightPercent="30%"
 
        app:layout_widthPercent="30%" />
 
  
 
  
 
    <View
 
        android:id="@+id/centre"
 
        android:layout_width="match_parent"
 
        android:layout_height="0dp"
 
        android:layout_below="@+id/top_left"
 
        android:background="#0000ff"
 
        app:layout_marginLeftPercent="10%"
 
        app:layout_marginRightPercent="20%"
 
        app:layout_marginTopPercent="10%"
 
        app:layout_marginBottomPercent="10%"
 
        app:layout_heightPercent="40%" />
 
  
 
    <View
 
        android:layout_width="match_parent"
 
        android:layout_height="0dp"
 
        android:id="@+id/bottom"
 
        android:layout_below="@+id/centre"
 
        android:background="#00f0ff"
 
        android:layout_alignParentLeft="true"
 
        android:layout_alignParentStart="true"
 
        app:layout_heightPercent="10%"/>
 
  
 
  
 
</android.support.percent.PercentRelativeLayout>
<ignore_js_op>
布局图1
下面我对百分比布局的属性进行了一些小实验,来验证我的几个问题:



1、layout_marginTopPercent这种类型的参数具体的意义是什么?

答:中间的蓝色view设置了我们疑惑的属性
[XML] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
<View
 
        android:id="@+id/centre"
 
        android:layout_width="match_parent"
 
        android:layout_height="0dp"
 
        android:layout_below="@+id/top_left"
 
        android:background="#0000ff"
 
        app:layout_marginLeftPercent="10%"
 
        app:layout_marginRightPercent="20%"
 
        app:layout_marginTopPercent="10%"
 
        app:layout_marginBottomPercent="10%"
 
        app:layout_heightPercent="40%" />
然后我们在看看实现的效果,请看布局图1。
再将layout_marginLeftPercent这类型的属性跟以前我们熟悉的layout_marginLeft类型属性对比可知,layout_marginLeftPercent属性是控件距离左边控件的距离为父类容器的宽度的百分比,其余属性类似。(其实这里不能证明是相对于父容器还是相对于屏幕而言,我们在下面进行证明)



2、控件设置的百分比是相对于屏幕还是相对于父容器而言呢?

答:从上面的实验代码我们基本了解了各种属性的使用,但是不禁疑惑百分比这个是相对父容器而言的吗?也许自然我们就会想到应该是相对于父容器,但是不在实践中证明始终都是不放心的。实践是证明真理的唯一标准!!
好的,其实要证明也很容易。下面我们对容器类的宽度和高度分别进行修改,然后看看显示的情况。
1)修改高度为定值300dp
[XML] 纯文本查看 复制代码
1
2
3
4
5
6
7
8
9
<android.support.percent.PercentRelativeLayout
 
 
 
    android:layout_width="match_parent"
 
android:layout_height="300dp">
然后我们看看显示的情况
<ignore_js_op>
2)修改宽度为200dp
[XML] 纯文本查看 复制代码
1
2
3
4
5
6
7
8
9
<android.support.percent.PercentRelativeLayout
 
 
 
    android:layout_width="200dp"
 
android:layout_height="match_parent">
再看看效果
<ignore_js_op>
实验结论:
从实验结果可知,百分比布局设置的属性都是相对于父容器而言的。

Android百分比布局支持库介绍——com.android.support:percent(转)的更多相关文章

  1. Android百分比布局支持库(android-percent-support)

    Android中提供了五种布局,其中用的最多的就是:LinearLayout, RelativeLayout 和 FrameLayout这三种布局,在对某一界面进行布局时最先想到也是通过这三种来布局的 ...

  2. Android百分比布局方案

    百分比布局让其中的控件在指定高度,宽度,margin时使用屏幕宽高的百分比,不使用dp,px.这样一套布局可以适应多个屏幕,方便适配.如: app:layout_heightPercent=" ...

  3. Android 百分比布局库(percent-support-lib) 解析与扩展

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/46695347: 本文出自:[张鸿洋的博客] 一.概述 周末游戏打得过猛,于是周 ...

  4. Android百分比布局成功导入及简单使用

    最近学习第一行代码第二版这本书,里面有介绍百分比布局的使用,经过一番摸索,终于是成功导入了百分比布局 就是这样,appcompat是25.3.1,那么百分比布局percent也是25.3.1 这样便是 ...

  5. [原]ffmpeg编译android 硬解码支持库 libstagefright

    最近花了一天时间将ffmpeg/tools/build_stagefright执行成功,主要是交叉编译所需要的各种动态库的支持没链接上,导致各种报错,基本上网络上问到的问题我都碰到了,特此记录下来. ...

  6. Android 屏幕适配(二)增强版百分比布局库(percent-support-lib)

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/46767825: 本文出自:[张鸿洋的博客] 一 概述 上周一我们发布了Andr ...

  7. Android 增强版百分比布局库 为了适配而扩展

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/46767825: 本文出自:[张鸿洋的博客] 一 概述 上周一我们发布了Andr ...

  8. android support Percent支持库开发

    Android的布局支持百分比的设置进行开发,来学习如何去实现它,不过看起来会像网页的设置,比如宽度的设置属性是`layout_widthPercent`.在此之前,我们一般都会设置Linearlay ...

  9. Android 支持库迁移到AndroidX

    一.背景 Android系统版本在不断更新,从最初的Android 1.0到现在Google和各大手机厂商正在推的Android 10,平均下来每个年头都有一个大的版本更新.但用户正在用的手机上的An ...

随机推荐

  1. HDU5785 Interesting(Manacher + 延迟标记)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5785 Description Alice get a string S. She think ...

  2. C#遍历enum类型

    对于enum类型: 使用foreach遍历enum类型的元素并填充combox foreach ( HatchStyle hs1 in Enum.GetValues(typeof(HatchStyle ...

  3. 如何终止java线程

    http://blog.csdn.net/anhuidelinger/article/details/11746365 终止线程的三种方法 有三种方法可以使终止线程. 1.  使用退出标志,使线程正常 ...

  4. BZOJ1012[JSOI2008]最大数maxnumber 题解

    题目大意: 维护一个数列,有两种操作:1. 查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度.2.插入操作:将n加上t,其中t是最近一次查询操作的答案(如果还未执行 ...

  5. 洛谷 P1092 虫食算 Label:dfs

    题目描述 所谓虫食算,就是原先的算式中有一部分被虫子啃掉了,需要我们根据剩下的数字来判定被啃掉的字母.来看一个简单的例子: 43#9865#045 +8468#6633 44445509678 其中# ...

  6. Codeforces Beta Round #3

    A题,水题,还是无法1Y. B题,题意是类似背包的问题,在v的容量下,有1重量和2重量的,如果达到价值最大. 贪心,写的很恶心.看着数据过了. 奇数的时候,先选一个1.之后然后1+1 和 2 比较就行 ...

  7. Android -- ids.xml文件的使用

    1.当我们在使用控件的时候,有的时候不可避免的要为控件声明id ,而分散在各个xml文件中,有时候查找起来又不是特别的方便 ,因此,安卓为我们提供了ids.xml 文件,保存在res -->va ...

  8. Android -- TextView、button方法详解(1)

    1.TextView常规方法 TextView myTextView=null; //声明变量 myTextView=(TextView)findViewById(R.id.myTextView); ...

  9. ffplay 播放yuv

    ffplay -f rawvideo -video_size 1920x1080 a.yuv ffplay -i raw_out2.yuv -pix_fmt yuv422p -s 1280x720

  10. Selenium_Selenium WebDriver 中鼠标和键盘事件分析及扩展

    在使用 Selenium WebDriver 做自动化测试的时候,会经常模拟鼠标和键盘的一些行为.比如使用鼠标单击.双击.右击.拖拽等动作:或者键盘输入.快捷键使用.组合键使用等模拟键盘的操作.在 W ...