android布局 layout
共有属性
- layout_width
- layout_height
- layout_margin 外边距
- padding 内边距
宽度
android:layout_width="200dp"
- match_parent
- wrap_content
- 200dp (字体的单位用sp)
线性布局
特点:从上往下,从左往右
重要属性
- android:orientation 方向
- android:layout_weight 权重
- android:layout_gravity 重力
android:orientation
描述整个布局
- vertical 垂直的
- horizontal 水平的
android:layout_weight
描述内部控件
假设现在有三个控件
方法一:
设置三个控件的宽度
剩下一个控件的layout_weight
设为1,那么可以理解为有layout_weight
的后摆放,占了剩下的全部空间
方法二:按比例划分
设置三个控件的宽度为0
按比例宽度设置控件的layout_weight
如下是按1:2:2 划分的效果
当内容多了时:
android:layout_gravity
描述内部控件
设置控件相对父容器往哪靠
下图是盒子1 center;盒子2 bottom
相对布局
相对于父容器
取值: true/false
靠父容器 边缘 四个方向:
- android:layout_alignParentLeft
- android:layout_alignParentRight
- android:layout_alignParentTop
- android:layout_alignParentBottom
居中:
- android:layout_centerInParent
- android:layout_centerHorizontal 水平居中
- android:layout_centerVertical 垂直居中
相对于其他控件
一)在参照物的哪一边
- android:layout_toLeftOf
- android:layout_toRightOf
- android:layout_above
- android:layout_below
二)和参照物的某一边对齐 alignXxx
UI基础控件 View
- 处理文本内容的View (TextView)
- 被点击的View ( Button )
- 处理图片内容的View(ImageView)
- 接收用户信息输入的View ( EditText )
- 进度条类的View(ProgressBar)
通用属性
属性 | 常用可选值 |
---|---|
android:layout_width android:layout_height | 1. match_parent 2. wrap_content 3. 正整数 dp |
android:id | @id/valName 使用已存在的Id @+id/valName 添加新id |
android:layout_margin | 正整数 dp |
android:padding | 正整数 dp |
android:backgroud | 十六进制颜色值 —— 颜色作为背景 @mipmap/resourceId —— 图片作为背景 |
android:layout_gravity (该控件 相对 父容器 的位置) android:gravity (该控件的内容 相对 控件 的位置) | center_ horizontal center vertical center left right top bottom |
android:visibility | visible 可见状态 invisible 不可见状态,但保留控件位置 gone 不可见状态,也不保留位置 |
TextView
相当于JLabel
android:text="" // 指定文本控件的文本内容
android:textSize="26sp" // 指定字体大小
android:textColor="#00ffff" // 指定字体颜色
android:lineSpacingExtra="15dp" // 行间距(具体大小)
android:lineSpacingMultiplier="1" // 行间距(倍数)
android:lines="" // 设置行数
android:single="true" // 设置单行
android:ellipsize="" // 设置省略号
android:focusable="true" // 设置可以获取焦点
android:focusableInTouchMode="true" // 设置在触摸时获取焦点
android:marqueeRepeatLimit="marquee_forever" // 设置跑马灯持续运行
EditView
输入框
android:inputType
输入类型
- textPassword 密码
- number 只能正整数
- numberSigned 整数
- numberDecimal 小数
android:hint
提示文字
android:maxLength
Button
Button注册点击事件的方法
-
自定义内部类
-
匿名内部类
-
当前Activity去实现事件接口
-
在布局文件xml中添加点击事件属性
<Button android:id="@+id/btn4" android:layout_width="match_parent" android:layout_height="60dp" android:layout_margin="30dp" android:text="通过在布局文件中添加点击事件属性" android:background="#cccccc" android:onClick="myClick" />
public void myClick(View v){ switch (v.getId()){ case R.id.btn4: Log.e("TAG","第四个按钮,xml"); break; case R.id.btn5: Log.e("TAG","第w五个按钮,xml"); break; } }
ImageView
- src —— 设置前景(不会随着宽高而被拉伸)
- backgroud —— 设置背景
ProgressBar
进度条