目录
1. Android的基础布局
2. LinearLayout 线性布局
3. RelativeLayout
4. 常用的控件
1. Android的基础布局
LinearLayout 线性布局
RelativeLayout 相对布局
TableLayout 表格布局
FrameLayout 帧布局(框架布局)
ConstrantLayout 约束布局 (Android Studio默认布局) 用于拖拽的
2. LinearLayout 线性布局
2.1 怎么将Android Studio默认的ConstrantLayout改为LinearLayout
1. 在design页面下->component tree->ConstrainLayout右键->Convert view...->选择LinearLayout 点击apply 1. 在code页面下->直接修改代码 将 androidx.constraintlayout.widget.ConstraintLayout 改为 LinearLayout
2.2 线性布局有两种:
-
水平的线性布局 所有控件都是水平挨个排布
如果没有android:orientation属性的存在
或者
android:orientation="horizontal"
-
垂直的线性布局 所有控件都是垂直挨个排布
android:orientation="vertical"
tips: 在android中,所有在页面上显示的东西,必须具备两个属性,这两个属性是宽和高
android:layout_width 宽度
Android:layout_height 高度
对于宽度和高度,他们的值有三个
1. wrap_content 按照内容自适应 1. match_parent 按照父容器尺寸填满 1. 50dp 数值(用的地方很单一)
2.3 比重:
android:layout_weight 如何算总比重: 看同一父亲且同一级的各个控件的weight 一旦weight属性生效,android:layout_width失效
2.4 布局排布:
android:gravity 内容位置改变 android:layout_gravity 本身位置改变
2.5 分隔线:内部的线
android:divider="@color/black" android:showDividers="middle"
2.6 嵌套线性布局结构
<LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"> <LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"></LinearLayout> </LinearLayout>
2.6属性
属性 | 说明 |
---|---|
android:id | 唯一值 |
android:layout_height | 高,wrap_content:(随内容变化,类似auto),match_parent:(同父元素一样)单元最好是:dp |
android:layout_width | 宽,同上 |
android:background | 背景色 |
android:layout_margin | 外边距 |
android:layout_padding | 内边距 |
android:orientation | horizontal水平排列(默认);vertical竖直排列 |
android:layout_weight | 权重 |
android:gravity | 排布方式 |
3. RelativeLayout
3.1基本框架:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/RelativeLayout1" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/img1" android:layout_width="80dp" android:layout_height="80dp" android:layout_centerInParent="true" android:src="@drawable/pic1"/> ... </RelativeLayout>
3.2属性:
-
相对于兄弟元素
属性名称 | 属性含义 |
---|---|
android:layout_below="@id/aaa" | 在指定View的下方 |
android:layout_above="@id/aaa" | 在指定View的上方 |
android:layout_toLeftOf="@id/aaa" | 在指定View的左边 |
android:layout_toRightOf="@id/aaa" | 在指定View的右边 |
android:layout_alignTop="@id/aaa" | 与指定View的上边界一致 |
android:layout_alignBottom="@id/aaa" | 与指定View下边界一致 |
android:layout_alignLeft="@id/aaa" | 与指定View的左边界一致 |
android:layout_alignRight="@id/aaa" | 与指定View的右边界一致 |
-
相对于父元素
属性名称 | 属性含义 |
---|---|
android:layout_alignParentLeft="true" | 在父元素内左边 |
android:layout_alignParentRight="true" | 在父元素内右边 |
android:layout_alignParentTop="true" | 在父元素内顶部 |
android:layout_alignParentBottom="true" | 在父元素内底部 |
-
对齐方式
属性名称 | 属性含义 |
---|---|
android:layout_centerInParent="true" | 居中布局 |
android:layout_centerVertical="true" | 垂直居中布局 |
android:layout_centerHorizontal="true" | 水平居中布局 |
-
间隔
属性名称 | 属性含义 |
---|---|
android:layout_marginBottom="" | 离某元素底边缘的距离 |
android:layout_marginLeft="" | 离某元素左边缘的距离 |
android:layout_marginRight ="" | 离某元素右边缘的距离 |
android:layout_marginTop="" | 离某元素上边缘的距离 |
android:layout_paddingBottom="" | 往内部元素底边缘填充距离 |
android:layout_paddingLeft="" | 往内部元素左边缘填充距离 |
android:layout_paddingRight ="" | 往内部元素右边缘填充距离 |
android:layout_paddingTop="" | 往内部元素右边缘填充距离 |
###
4. 常用的控件
TextView 文本控件 给用户一个文字性的提示
EditText 输入文本控件
ImageView 图片控件 显示图片
Button 按钮
TextView
-
掌握常用的属性
-
掌握资源文件的使用
如何创建资源文件,并对资源文件编程-
-
store:设置边框
corners:设置边框弧形半径
gradient:设置渐变色
solid:设置填充色
padding:设置内边距
-
使用资源文件
android:background="@drawable/设置的名字"
-
-
跑马灯 (自己探索)
-
带图片的TextView
android:drawableTop :在文字上边 android:drawableBottom :在文字下边 android:drawableLeft : 在文字左边 android:drawableRight: 在文字右边
重点1:怎么样设置一个shaper(皮肤)
在drawable文件夹下设置 xml
重点2:带图片的TextView