Android客户端 | 青训营笔记

76 阅读2分钟

这是我参与「第四届青训营 -Android场」笔记创作活动的的第3天

常规&高级UI编程

主要内容

UI组件 布局 渲染 交互 动画 自定义UI

UI组件

图形用户界面

UI界面由多个不同功能的UI组件构成

Android SDK提供了大量的UI组件

组件JavaClassPackage
文本组件Text Viewandroid.widget.TextView
图片组件ImageViewandroid.widget.ImageView
按钮组件Buttonandroid.widget.Button
输入框组件EditTextandroid.widget.EditText
复选框组件CheckBoxandroid.widget.CheckBox
单选按钮RadioButtonandroid.widget.RadioButton

常规组件大多由Android Framework中的android.widge这个package提供

常规view的属性和方法

image.png

常规UI组件的使用

TextView
android:id="@+id/tv_title"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_margin="20dp"
android:layout_gravity="center"
android:gravity="center"
android:padding="10dp"
android:text="@string/app_name"
android:textSize="20sp"
android:textColor="@color/colorAccent"
android:visibility="visible"
android:background="@color/colorPrimary"

image.png

高级UI组件

组件JavaClass
滑动组件ScrollView
列表组件ListView/RecyclerView
下拉刷新组件PullToRefresh
分页组件ViewPager
布局组件LinearLayout/RealtiveLAyout/...

常规UI组件大多是View,高级UI组件大多是ViewGroup,比常规UI组件有多的功能

UI组件关系

View和ViewGroup的关系 image.png 部分View的继承关系 image.png

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"

EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/to"

EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"

EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:hint="@string/message"

Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="@string/send"

布局

LinearLayout

属性功能描述
android:orientation布局内组件的排列方向
android:layout_weight布局内组件大小权重
android:divider布局内组件间分割线
android:showDividers布局内组件间分割线位置
android:dividerPadding布局内分割线padding

示例

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical"

EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"

EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"

EditText
android:layout_width=".match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"

Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"

image.png

RelativeLayout

image.png

示例

RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="math_parent" 
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"

EditText
android:id="@+id+name"
android:layout_width="match_parent"
android:layout_height="wrap_content"

Spinner
android:id="@+id/dates"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/times"

Spinner
android:id="@id/times"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_alignParentRight="true"

Button
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/times"
android:layout_alignParentRight="true"

image.png

FrameLayout

image.png 前景图像:永远处于FrameLayout最上层,不会覆盖图片

xmlns:android="http:/schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"

TextView
android:layout_width="300dp"
android:layout_height="3000dp"
android:gravity="center"
android:background="@android:color/holo_blue_bright"
android:text="我是第一层"

TextView
android:layout_width="150dp"
android:layout_height="140dp"
android:gravity="center"
android:background="@android:color/holo_green_light"
android:text="我是第二层"

image.png

ConstraintLayout

image.png

示例

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"

ImageView
android:id="@+id/iv_beauty"
android:layout_width="100dp"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="@drawable/beauty"
app:layout_constrainLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"

ImageView
android:id="@+id/iv_girl"
android:layout_width="100dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
android:src="@drawable/girl"
app:layout_constraintBottom_toBottomOf="@+id/iv_beauty"
app:layout_constraintLeft_toRightOf="@+id/iv_beauty"
app:layout_constraintTop_toTopOf="parent"

image.png

布局总结

image.png