Android Studio 控件的百分比布局

Stella981
• 阅读 559
  • 在安卓开发控件的百分比布局学习中,由于我参考的是第一行代码中的资源,其中的Android studio版本过低,导致的一些错误,花了好长时间来解决它。这里我用的是Android studio 3.5.3

Android Studio 控件的百分比布局

首先是依赖的添加,由于我使用的版本相比参考书中的高,Android studio中已经不支持在compile,****所以我在模块级build.gradle添加如下的依赖:

1 implementation 'androidx.percentlayout:percentlayout:1.0.0'

1 dependencies {
2     implementation 'androidx.percentlayout:percentlayout:1.0.0'
3     implementation fileTree(dir: 'libs', include: ['*.jar'])
4     implementation 'androidx.appcompat:appcompat:1.1.0'
5     implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
6     testImplementation 'junit:junit:4.12'
7     androidTestImplementation 'androidx.test.ext:junit:1.1.0'
8     androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
9 }

 后面就是对layout中的 *.xml 文件进行修改,****这里的话百分比布局中分 PercentRelativeLayout PercentFrameLayout 两种****我采用的是PercentRelativeLayout,代码如下

 1 <androidx.percentlayout.widget.PercentRelativeLayout
 2     xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent">
 6 
 7     <TextView
 8         android:id="@+id/row_one_item_one"
 9         android:layout_width="0dp"
10         android:layout_height="0dp"
11         android:layout_alignParentTop="true"
12         android:background="#7700ff00"
13         android:text="w:70%,h:20%"
14         android:gravity="center"
15         app:layout_heightPercent="20%"
16         app:layout_widthPercent="70%"/>
17 
18     <TextView
19         android:id="@+id/row_one_item_two"
20         android:layout_width="0dp"
21         android:layout_height="0dp"
22         android:layout_toRightOf="@+id/row_one_item_one"
23         android:background="#396190"
24         android:text="w:30%,h:20%"
25         app:layout_heightPercent="20%"
26         android:gravity="center"
27         app:layout_widthPercent="30%"/>
28 
29 
30     <ImageView
31         android:id="@+id/row_two_item_one"
32         android:layout_width="match_parent"
33         android:layout_height="0dp"
34         android:src="@drawable/img_1"
35         android:scaleType="centerCrop"
36         android:layout_below="@+id/row_one_item_one"
37         android:background="#d89695"
38         app:layout_heightPercent="70%"/>
39 
40     <TextView
41         android:layout_width="0dp"
42         android:layout_height="0dp"
43         android:layout_below="@id/row_two_item_one"
44         android:background="#770000ff"
45         android:gravity="center"
46         android:text="width:100%,height:10%"
47         app:layout_heightPercent="10%"
48         app:layout_widthPercent="100%"/>
49 
50 
51 </androidx.percentlayout.widget.PercentRelativeLayout>

这里我参照了一位博友的博客

http://blog.csdn.net/lmj623565791/article/details/46695347

想了解其中的源码分析可以参考这个博客。

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Stella981 Stella981
2年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Easter79 Easter79
2年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Stella981 Stella981
2年前
Android开发问题汇总
1.ClassCastException异常是类型匹配出现的错误,xml布局文件中的控件id在Activity中匹配错误2.eclipse中遇到logcat无任何信息输出解决办法:windowshowview选择android下的devices,打开devices,点击右边的截屏图片。
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这