1.Android最常用的布局主要包括FrameLayout(框架布局)linerLayout(线性布局)AbsoluteLayout(绝对布局)RelativeLayout(相对布局)以及tableLayout(表格布局)
其中最为常用的是线性布局以及相对布局,这两种布局方式能避免产生因为各种的屏幕的分辨率的不同而造成的Aciviy显示变形等各种屏幕适配的问题。
2.一些属性的区别:比如layout_gravity gravity等的属性的区别:前者指的是这个空间在父控件中所要显示的位置。后者通常指的是控件中的文本内容在该控件中所显式的位置。这里要注意如下的一种情况。
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="程序管理器"
android:textColor="#66ff00"
android:textSize="28sp" />
和第二种情况相似的另外一个情况:padding和margin的区别:
padding是站在父view的角度描述问题,它规定它里面的内容必须与这个父view边界的距离。margin则是站在自己的角度描述问题,规定自己和其他(上下左右)的view之间的距离,如果同一级只有一个view,那么它的效果基本上就和padding一样了。
3.layout_weight:这个主要指的是控件的重要程度,要根据layout_width的属性而定:比如如下的情况:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button2"
/>
</LinearLayout>
出现的布局是:button1占了2/3,button2占了1/3。
但是另外一种情况:
但是如果将布局文件中的button的属性android:layout_width="fill_parent"改为android:layout_width="wrap_content"那么出现的结果为:button1占了1/3,button2占了2/3。
具体的实现原理个人也不是很清楚,但是可以死记为如下的规则:
当layout_width:fill_parent的时候,数值小的重要度反而更大,但是如果属性值为wrap_content的时候,正好相反,但是不管哪种情况,数值大的多的那个占据的空间一般都比较大。
RelativeLayout的几个常见的属性(分为了几种不同的情况,利于区别):
RelativeLayout ( 相对布局 ) : (里面可以放多个控件,但是一行只能放一个控件)
附加几类 RelativeLayout 的属性供大家参考:
第一类 : 属性值为 true 或 false
android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相对于父元素完全居中
android:layout_alignParentBottom 贴紧父元素的下边缘
android:layout_alignParentLeft 贴紧父元素的左边缘
android:layout_alignParentRight 贴紧父元素的右边缘
android:layout_alignParentTop 贴紧父元素的上边缘
android:layout_alignWithParentIfMissing 若找不到兄弟元素以父元素做参照物
第二类:属性值必须为 id 的引用名“ @id/id-name ”
android:layout_below 在某元素的下方
android:layout_above 在某元素的上方
android:layout_toLeftOf 在某元素的左边
android:layout_toRightOf 在某元素的右边
android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐
第三类:属性值为具体的像素值,如 30dip , 40px
android:layout_marginBottom 离某元素底边缘的距离
android:layout_marginLeft 离某元素左边缘的距离
android:layout_marginRight 离某元素右边缘的距离
android:layout_marginTop 离某元素上边缘的距离关于布局需要注意如下的两点:
注意事项:
1 、各布局不要乱用各自的属性。比如把属于 AbsoluteLayout 布局的android:layout_x和android:layout_y用到 LinearLayout 布局或 RelativeLayout 布局,或者把 RelativeLayout 布局的 below , rightof 等属性应用到其他布局中。这样做虽然不会报错,但这是白浪费感情的工作,根本达不到我们需要的效果。
2 、关于android:layout_width="fill_parent" 和 android:layout_height="wrap_content" ,这是对每个布局宽和高的设置。 wrap_content 可表示随着其中控件的不同而改变这个布局的宽度或高度,类似于自动设置宽和高, fill_parent 使布局填充整个屏幕,另外还有一种 match_parent ,它本质上和 fill_parent 一样,并从 API Level8 开始替代 fill_parent 。