android 布局有 5 大布局方式,线性布局、相对布局、绝对布局、帧布局、表格布局 5 中,现在做下个人的总结
1. 线性布局
线性布局是从上往下(垂直)布局,或从左往右(水平)布局的方式。因此必备属性 android:orientation 确定布局方向是 vetical 或者 horizontal。另外 android:width,android:height 属性确定此布局在容器中的位置。
View Code
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
- android:orientation="vertical"
- android:layout_width="match_parent" android:layout_height="match_parent"
- tools:context="com.example.mylearn.activity.MainActivity">
- <Button
- android:text="自定义控件"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/topbar"/>
- <Button
- android:text="webview浏览网页"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/webview_btn" />
- <Button
- android:text="imageview浏览图片"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/imageview_btn"/>
- </LinearLayout>
控件与界面边缘之间的间隙,控件之间的间隙是系统自身的控件造成的,如果是自定义控件,就不会出现这种问题。设置控件的 android:background 属性为红色就可以验证此现象
2. 相对布局
相对布局是通过设置容器中的控件相对位置进行布局。对于此中布局,RelativeLayout 元素节点的属性无非就是设置布局的宽,高,内 padding,外 margin 等,重点在于布局内部的控件的属性设置上,可以分为一下四方面来记忆:
第一类: 属性值必须为 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 本元素的右边缘和某元素的的右边缘对齐
第二类: 属性值为 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 如果对应的兄弟元素找不到的话就以父元素做参照物
第三类: 属性值为具体的像素值
- android:layout_marginBottom 离某元素底边缘的距离
- android:layout_marginLeft 离某元素左边缘的距离
- android:layout_marginRight 离某元素右边缘的距离
- android:layout_marginTop 离某元素上边缘的距离
第四类: 其他
EditText 的 android:hint 设置 EditText 为空时输入框内的提示信息。
android:gravity android:gravity 属性是对该 view 内容的限定.比如一个 button 上面的 text. 你可以设置该 text 在 view 的靠左,靠右等位置.以 button 为例,
android:gravity="right" 则 button 上面的文字靠右
android:layout_gravity android:layout_gravity 是用来设置该 view 相对与起父 view 的位置.比如一个 button 在 linearlayout 里,你想把该 button 放在靠左、靠右等位置
就可以通过该属性设置.以 button 为例,android:layout_gravity="right" 则 button 靠右
来源: http://www.bubuko.com/infodetail-1962655.html