【个人总结】Android几种常用布局的总结
16lz
2021-01-26
1、线性布局(LinearLayout):框架内的元素在横向或纵向呈线性排布。该框架可通过orientation控制现线性方向,通过layout_weight控制一行中各个元素的比例。一个LinearLayout元素就是一行或一列(看orientation决定)。
2、框架布局(FrameLayout):该框架是一个层叠的布局,放上去的元素是呈一层一层分布的,主要用来放置需要重叠的元素,比如图片等。
3、相对布局(RelativeLayout):最常用的一种布局,可根据控制元素与元素的相对距离,相对比较灵活,一般要决定一个元素的距离,需要两个相对位置才能比较准确确定,就跟两点坐标确定一个距离位置是一个道理。控制相对位置的属性有很多,常用的有toLeftOf,toRightOf,above,below,alignLeft,alignRight,alignTop,alignBottom等等。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" android:paddingBottom = "@dimen/activity_vertical_margin" android:paddingLeft = "@dimen/activity_horizontal_margin" android:paddingRight = "@dimen/activity_horizontal_margin" android:paddingTop = "@dimen/activity_vertical_margin" android:orientation = "horizontal" tools:context = ".LinearLayoutActivity" > < LinearLayout android:layout_width = "100dp" android:layout_height = "wrap_content" android:layout_weight = "3" android:background = "#ff0000" android:orientation = "vertical" >" < TextView android:layout_width = "match_parent" android:layout_height = "wrap_content" android:text = "@string/hello_world" android:background = "#00ff00" /> < TextView android:layout_width = "match_parent" android:layout_height = "wrap_content" android:text = "@string/hello_world" android:background = "#0000ff" /> </ LinearLayout > < LinearLayout android:layout_width = "100dp" android:layout_height = "wrap_content" android:layout_weight = "1" android:background = "#00ff00" android:orientation = "vertical" > < TextView android:layout_width = "match_parent" android:layout_height = "wrap_content" android:text = "@string/hello_world" android:background = "#ff0000" /> < TextView android:layout_width = "match_parent" android:layout_height = "wrap_content" android:text = "@string/hello_world" android:background = "#0000ff" /> </ LinearLayout >" </ LinearLayout > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <!-- FrameLayout内的元素布局的方法有限,只要是控制是否居中和控制与边缘的距离 --> < FrameLayout android:layout_width = "300dp" android:layout_height = "300dp" android:layout_below = "@id/button4" android:layout_alignLeft = "@id/button4" android:paddingBottom = "@dimen/activity_vertical_margin" android:paddingLeft = "@dimen/activity_horizontal_margin" android:paddingRight = "@dimen/activity_horizontal_margin" android:paddingTop = "@dimen/activity_vertical_margin" android:background = "#000000" > < ImageView android:id = "@+id/kobe" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:background = "@drawable/kobe" android:scrollbars = "vertical" android:contentDescription = "kobe" /> < ImageView android:id = "@+id/robot" android:layout_width = "50dp" android:layout_height = "50dp" android:layout_gravity = "center" android:src = "@drawable/androidrobot" /> </ FrameLayout > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" android:paddingBottom = "@dimen/activity_vertical_margin" android:paddingLeft = "@dimen/activity_horizontal_margin" android:paddingRight = "@dimen/activity_horizontal_margin" android:paddingTop = "@dimen/activity_vertical_margin" tools:context = ".MainActivity" > < Button android:id = "@+id/button1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "button1" /> < Button android:id = "@+id/button2" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignBottom = "@id/button1" android:layout_toRightOf = "@id/button1" android:text = "button2" /> < Button android:id = "@+id/button3" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignBottom = "@id/button2" android:layout_toRightOf = "@id/button2" android:text = "button3" /> < Button android:id = "@+id/button4" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignLeft = "@id/button1" android:layout_below = "@id/button1" android:text = "button4" /> < Button android:id = "@+id/button5" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignBottom = "@id/button4" android:layout_toRightOf = "@id/button4" android:text = "button5" /> < Button android:id = "@+id/button6" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignBottom = "@id/button5" android:layout_toRightOf = "@id/button5" android:text = "button6" /> </ RelativeLayout > |
4、表格布局(TableLayout): 该布局是继承了线性布局,可控制多行多列,可控制行是否可隐藏,是否可伸缩。StretchColumn表示可伸展列,元素不够列宽度自动伸展补齐,ShrinkColumn表示可收缩列,元素不够或超出列宽度会自动收缩,CollapseColumn表示可隐藏列。可通过这几个属性控制有几列,然后加入子标签<TableRow>添加行,几个<TableRow>表示几行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | < TableLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" android:stretchColumns = "0,1,2" android:paddingBottom = "@dimen/activity_vertical_margin" android:paddingLeft = "@dimen/activity_horizontal_margin" android:paddingRight = "@dimen/activity_horizontal_margin" android:paddingTop = "@dimen/activity_vertical_margin" tools:context = ".MainActivity" > <!-- 应为该布局采用的是stretchColumn伸展列,所以如果每列的元素宽度不够会自动延伸使之充满每列的宽度 --> < TableRow android:id = "@+id/tr1" android:layout_width = "match_parent" android:layout_height = "match_parent" android:background = "#234875" > < Button android:id = "@+id/button1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "1" /> < Button android:id = "@+id/button2" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "2" /> < Button android:id = "@+id/button3" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "3" /> </ TableRow > <!-- 没定义TableRow来控制每一行则会把该元素当做是一整行的内容 --> < Button android:id = "@+id/button4" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "4" /> < Button android:id = "@+id/button5" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "4" /> </ TableLayout > |
更多相关文章
- [置顶] 【Android】毫无耦合性,一个Item根布局搞定 item侧滑删除
- Android布局绘制常见小问题
- XML的解析中的三种方法
- android 布局方式 像素单位
- Android布局管理器-使用LinearLayout实现简单的登录窗口布局
- Android新控件MotionLayout介绍(一)
- android 修改AlertDialog的黑色背景的两种方式及圆角边框的设置
- Android中VideoView播放视频不能充满屏幕以及视频上的view与视频
- “layer-list” 和 “include”的使用