达内课程-布局控件之相对布局和线性布局
来源:互联网 发布:网易域名邮箱注册 编辑:程序博客网 时间:2024/06/09 19:47
相对布局RelativeLayout
线性布局LinearLayout
表格布局TableLayout
帧布局FrameLayout
网格布局GridLayout
RelativeLayout
停靠父控件边界
alignParentTop
alignParentBottom
alignParentLeft
alignParentRight
相对父控件居中
centerInParent
centerHorizontal
centerVertical
停靠周围控件边界
above
below
toRightOf
toLeftOf
与周围控件边界对齐
alignTop
alignBottom
alignLeft
alignRight
alignBaseLine
练习
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.xy.csdnproject.RelativeLayoutActivity"> <!-- @+id新定义id 在R类中新定义一个整数常量 如果常量已存在就引用现有常量 @id引用id 在引用id时也可以使用@+id 但是如果写错的已存在的id就会新创建一个整数常量 所以为了严谨应该用@id --> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="btn1"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:text="btn2"/> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:text="btn3"/> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:text="btn4"/> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="btn5"/> <Button android:id="@+id/button6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:text="btn6"/> <Button android:id="@+id/button7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:text="btn7"/> <Button android:id="@+id/button8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="btn8"/> <Button android:id="@+id/button9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:text="btn9"/> <Button android:id="@+id/button10" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button1" android:layout_toRightOf="@id/button1" android:layout_above="@id/button4" android:layout_alignRight="@id/button2" android:text="btn10"/> <Button android:id="@+id/button11" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@id/button4" android:layout_toRightOf="@id/button4" android:layout_toLeftOf="@id/button5" android:layout_above="@id/button7" android:text="btn11"/> <Button android:id="@+id/button12" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@id/button11" android:layout_toRightOf="@id/button11" android:layout_toLeftOf="@id/button6" android:text="btn12"/></RelativeLayout>
LinearLayout
布局方向
orientation=”horizontal” 水平(默认)
orientation=”vertical” 垂直
比重,比例
1、按比例来分配剩余可用空间
layout_weight=”1”
layout_weight=”2”
2、如果让控件大小严格按照比例显示,可以把高度或宽度设置为“0dp”
重力
layout_gravity=”可选值”
水平布局中可选值
top
bottom
center_vertical
垂直布局中可选值
left
right
center_horizontal
center
练习
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.xy.csdnproject.LinearLayoutActivity" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="btn1" android:layout_weight="2"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="btn2"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="btn3" android:layout_weight="1"/></LinearLayout>
这样设置,btn1和btn2的总高度不是2:1,因为按钮原来有一个高度,所谓的2:1是剩余高度比例
如果想让btn1和btn2的总高度比例是2:1应把高度设置为0dp
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.xy.csdnproject.LinearLayoutActivity" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="btn1" android:layout_weight="2"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="btn2"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="btn3" android:layout_weight="1"/></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.xy.csdnproject.LinearLayoutActivity" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="btn1" android:layout_weight="2"/> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" android:background="#ccc"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="btn4" android:layout_gravity="center_vertical"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:text="btn5" android:layout_weight="1" android:layout_gravity="bottom"/> </LinearLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="btn2"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="btn3" android:layout_weight="1"/></LinearLayout>
阅读全文
0 0
- 达内课程-布局控件之相对布局和线性布局
- 达内课程-布局控件之GridLayout
- 达内课程-布局控件之TableLayout和FrameLayout
- Android 五大布局之线性布局和相对布局
- 线性布局和相对布局的知识点
- 线性布局和相对布局小Demo
- Android 线性布局和相对布局
- Android相对布局和线性布局
- Android相对布局和线性布局
- Android 布局(一):相对布局和线性布局
- 安卓布局:线性布局(Linearlayout)和相对布局(RelativeLayout)
- 线性布局 相对布局 参数
- Android 五大布局之(一) 线性布局和相对布局
- Android studio布局方式之线性布局和相对布局的结合
- 布局——线性布局、相对布局
- Android 布局之线性布局、相对布局、表格布局、帧布局
- 相对布局的控件布局
- android之线性布局,相对布局属性总结
- pandas入门——文件读取与写入
- 优酷播放按钮动画原理解析
- java代码实现CSV文件读取、将数据拆分成多个CSV文件及数据导出到CSV文件
- 函数表达式---闭包
- 命令行编辑 快捷键
- 达内课程-布局控件之相对布局和线性布局
- 《视频直播技术详解》系列:(7)推流和传输
- HDU 6096 String(2017 Multi-University Training Contest 6)
- 全排列及相关扩展算法(二)——求字典序下一组排列及全排列算法
- StringUtils中isEmpty 和isBlank的区别
- 如何部署 OS X 上 Sublime Text 3 + LaTeX 的中文环境?
- JavaScript块级作用域与函数作用域
- jquery操作复选框(checkbox)的方式
- 【GDOI2018模拟8.14】神奇的矩阵