Android 常规 & 高级 UI 编程 | 青训营笔记

144 阅读1分钟

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

Android UI组件

是什么?

UI:User Interface

图形用户界面

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

Android SDK 提供了大量的UI组件

有哪些?

输入框组件:EditText

按钮组件:Botton

文本组件:TextView

播放器组件:VideoView

图片组件:ImageView

列表组件:RecyclerView

常规UI组件的属性和方法

通用属性

id:设置组件的id

height:设置组件的高度

width:设置组件的宽度

margin:设置组件的外边距

padding:设置组件的内边距

通用方法

setID(@IdRes int id)

setLayoutParams(ViewGroup.LayoutParams params)

高级UI组件

滑动组件:ScrollView

列表组件:ListView、RecyclerView

下拉刷新组件:PullToRefresh

分页组件:ViewPager

布局

Android UI | 青训营笔记 - 掘金 (juejin.cn)

渲染

布局加载

编写布局文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".ui.MainActivity">
    
    ···
    
</androidx.constraintlayout.widget.ConstraintLayout>

注册Manifest

AS自动完成注册

<activity
    android:name=".ui.MainActivity"
    android:exported="true"
    android:launchMode="singleTask"
    android:taskAffinity="com.***.***">
      <intent-filter>
          <action android:name="android.intent.action.MAIN" />
           <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
</activity>

设置布局文件

override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
}

页面绘制起点

为什么Activity 在 onResume() 之后才显示?

onCreate :

setContentView() 创建了 DecorView,并将layout中的View 添加至 DecorView 中

onResume() : ActivityThread.handleResumeActivity()

  1. WindowsMangerImpl.addView
  2. 创建 ViewRootImpl
  3. ViewRootImpl.setView
  4. ViewRootImpl.requestLayout(),触发页面绘制