Android开发之百分比布局
16lz
2021-01-24
一个丰富的界面总是要由很多个控件组成的,那我们如何才能让各个控件有条不紊的摆放在界面上,而不是乱糟糟的呢?这就需要借助布局来实现啦。Android中四种基本布局:a,LinearLayout 线性布局b,RelativeLayout 相对布局c,FramLayout 帧布局d,PercentLayout 百分比布局Android引入的这种全新的布局方式,我们不在使用wrap_content,match_parent等方式来指定控件的大小,而是允许直接指定控件在布局中所占的百分比。
如何使用呢?
1,打开app/build.gradle文件,在dependencies闭包中添加:
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:percent:24.2.1' compile 'com.android.support:appcompat-v7:24.2.1'}
2,在main_activity.xml中编写:
<?xml version="1.0" encoding="utf-8"?>**"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">
如果你使用的是老版本的Android Studio,可能会在布局中看到错误的提示信息:
'layout_height' attribute should be defined more... (Ctrl+F1) 'layout_width' attribute should be defined more... (Ctrl+F1)
这是因为老版本中内置了布局的检查机制,认为每一个控件都应该通过宽度和高度属性来指定宽高才是合法的。这个错误的提示不会影响程序的运行,我们可以直接忽视掉。
看一下效果图:
更多相关文章
- 《解析 Google Android(安卓)DSK-智能手机开发范例手册》.(薛显
- Android属性动画特点详解
- Android(安卓)自定义弹幕控件
- 浅谈Android五大布局——LinearLayout、FrameLayout、AbsoulteLa
- Android中长度单位和边距
- 简化Android的UI开发
- 布局概述之帧布局FrameLayout和布局概述之表格布局TableLayout
- Android(安卓)ListView异步加载图片乱序问题,原因分析及解决方案
- Android之富有动感的底部弹窗效果