android学习笔记(2.1)--7.20--(5中布局方式+android单位)
来源:互联网 发布:服务器机柜和网络机柜 编辑:程序博客网 时间:2024/05/24 07:40
五大布局Layout:
LinearLayout 线性布局
RelativeLayout 相对布局
AbsoluteLayout 绝对布局
FrameLayout 帧布局
TableLayout 表格布局
1.LinearLayout线性布局:
LinearLayout 线性布局
android:orientation="horizontal" 制定线性布局的排列方式
水平 horizontal
垂直 vertical
android:gravity 控制当前控件内容显示区域
android:gravity意思是这个控件自己的“重力”,在通俗点就是控件上面的东西的位置(图片,文本等)
举个例子:一个TextView里面的文本默认居左作对齐的,你想让这些文本居中的话,只要在这个TextView的属性里加上android:gravity="center"
所有的参数分别是center(居中),bottom(下),top(上),right(右),left(左)
其他的一些是不常用的上面这些足够用了,比如要弄一个左下的android:gravity=" left| bottom "就OK了
layout_gravity 当前控件在父元素的位置
Layout_weightSum
Layout_weight 额外空间分配(权重)
首先计算出额外空间(可以为负)如果额外空间不为0并且有子View的layout_weight不为0的话按layout_weight分配额外空间:在控件大小安原有的大小设置完成后,
如果还有额外空间,那么可以用来分配给设置Layout_weight的控件。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第一个"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" <strong><span style="color:#ff0000;"> android:layout_weight="1"</span></strong> android:text="第二个"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第三个"/></LinearLayout>
Layout_weightSum不指定时安子控件的Layout_weight相加决定
android:visibility="invisible"控制布局是否显示
显示 visible
不显示,但占空间 invisible
隐藏 gone
2. RelativeLayout 相对布局:
android:layout_toRightOf在指定控件的右边
android:layout_toLeftOf 在指定控件的左边
android:layout_above 在指定控件的上边
android:layout_below 在指定控件的下边
android:layout_alignBaseline 跟指定控件水平对齐
android:layout_alignLeft 跟指定控件左对齐
android:layout_alignRight 跟指定控件右对齐
android:layout_alignTop 跟指定控件顶部对齐
android:layout_alignBottom 跟指定控件底部对齐
android:layout_alignParentLeft 是否跟父布局左对齐
android:layout_alignParentTop 是否跟父布局顶部对齐
android:layout_alignParentRight 是否跟父布局右对齐
android:layout_alignParentBottom 是否跟父布局底部对齐
android:layout_centerVertical 在父布局中垂直居中
android:layout_centerHorizontal 在父布局中水平居中
android:layout_centerInParent 在父布局中居中
3. AbsoluteLayout 绝对布局:
android:layout_x指定控件在父布局的x轴坐标
android:layout_y指定控件在父布局的y轴坐标
p.s 原点在左上角
4. FrameLayout 帧布局:
帧布局每次添加的控件都显示在最上面,最后显示在界面上的是最后添加的一个控件,可以理解为Z轴值不同,后加的在上面。
5. TableLayout 表格布局:
android:shrinkColumns 收缩列
android:stretchColumns 拉伸列
android:collapseColumns 隐藏列
android:layout_column 指定列(作用在列的身上)
android:layout_span 合并列(作用在列的身上)
<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" <span style="color:#ff0000;">android:shrinkColumns="1"</span>> <TableRow > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第0行第0列"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第0行第1列"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第0行第2列"/> </TableRow><TableRow > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第1行第1列" <strong><span style="color:#ff0000;"> android:layout_column="1" android:layout_span="2"</span></strong> /></TableRow> </TableLayout>
android单位:
px (pixels)像素:
一般HVGA代表320x480像素,这个用的比较多。
dip或dp (device independent pixels)设备独立像素:
这个和设备硬件有关,一般为了支持WVGA、HVGA和QVGA 推荐使用这个,不依赖像素。(使用dp可以理解为320*480的px,会自动适配)
sp (scaled pixels — best for text size)比例像素:
主要处理字体的大小,可以根据用户系统的字体自适应。
除了上面三个显示单位,下面还有几个不太常用:
in (inches)英寸
mm (millimeters)毫米
pt (points)点,1/72英寸
为了适应不同分辨率,不同的像素密度,推荐使用dip ,文字使用sp。
P.S:
常见的密度比值:
240*320 的密度比值是: 0.75
320*480 的密度比值是: 1.0
480*800 的密度比值是: 1.5
float density = getResources().getDisplayMetrics().density;
1.0 * 160dp = 160px
0.75 * 160dp = 120px
1.5 * 160dp = 240px
- android学习笔记(2.1)--7.20--(5中布局方式+android单位)
- 安卓学习笔记(六)、Android中长度单位
- android 布局方式 像素单位
- android 布局方式 像素单位
- Android学习笔记(二)距离单位
- Android中常用布局单位
- Android学习笔记(16):绝对布局AbsoluteLayout、常用距离单位
- Android学习笔记--尺寸单位
- Android开发学习笔记:5大布局方式详解
- Android开发学习笔记:5大布局方式详解
- Android开发学习笔记:5大布局方式详解
- Android开发学习笔记:5大布局方式详解
- Android开发学习笔记:5大布局方式详解
- Android开发学习笔记:5大布局方式详解
- Android开发学习笔记:5大布局方式详解
- Android学习之布局常用的单位
- Android中布局的像素和单位
- android xml 布局单位
- 我的CSDN博客世界:欢迎切磋和交流
- HDU 4417 Super Mario
- 【IPV6基础知识】IPV6地址结构
- 1033. To Fill or Not to Fill
- Ubuntu 配置 Tomcat与日常错误解决
- android学习笔记(2.1)--7.20--(5中布局方式+android单位)
- Ubuntu字典app开发(二)——探索与实践
- poj 2240 Arbitrage
- java压缩处理Image图片
- hdu 1257 动态规划
- hdu 1420 Prepared for New Acmer(快速幂入门)
- GetLastError函数返回值的含义
- 数组排序 链表
- Java语言中关键字strictfp的用途