IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> android.view.View -> 正文阅读

[移动开发]android.view.View

android.view.View

主要作用是drawing,event handling

常用方法

Set properties...
requestFocus
setOnClickListener
setVisibility(int)

实现自定义View

Category Methods
Creation Constructors
onFinishInflate()

Layout onMeasure(int, int)
onLayout(boolean, int, int, int, int)
onSizeChanged(int, int, int, int)

Drawing onDraw(Canvas)
Event processing onKeyDown(int, KeyEvent)
onKeyUp(int, KeyEvent)
onTrackballEvent(MotionEvent)
onTouchEvent(MotionEvent)
Focus onFocusChanged(boolean, int, Rect)
onWindowFocusChanged(boolean)

Attaching onAttachedToWindow()
onDetachedFromWindow
onWindowVisibilityChanged(int)

一般覆写上面的方法,由Framework调用。

Position

getLeft() and getTop() 返回基于父组件左上角的位置。

Size,padding and margins

Size: 两对宽高。

  1. 测量后期望的宽高:These dimensions define how big a view wants to be within its parent (see Layout for more details.) The measured dimensions can be obtained by calling getMeasuredWidth() and getMeasuredHeight().
  2. 在屏幕上显示出的实际宽高: getWidth() and getHeight()

padding: setPadding(int, int, int, int) & getPaddingTop(), getPaddingRight() …

margin: view groups provide such support.

Layout

2 process: measure and layout.

measure(int, int)执行后,getMeasuredWidth()一定有返回值

测量过程必须以父组件的约束为前提,

layout(int, int, int, int)

父组件由上到下推送测量的要求:

MeasureSpecs are used to push requirements down the tree from parent to child. A MeasureSpec can be in one of three modes:

  • UNSPECIFIED: This is used by a parent to determine the desired dimension of a child view. For example, a LinearLayout may call measure() on its child with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how tall the child view wants to be given a width of 240 pixels.
  • EXACTLY: This is used by the parent to impose an exact size on the child. The child must use this size, and guarantee that all of its descendants will fit within this size.
  • AT_MOST: This is used by the parent to impose a maximum size on the child. The child must guarantee that it and all of its descendants will fit within this size.

子View 表达自己的期望用LayoutParam:

  • an exact number
  • MATCH_PARENT, which means the view wants to be as big as its parent (minus padding)
  • WRAP_CONTENT, which means that the view wants to be just big enough to enclose its content (plus padding).

Drawing

To force a view to draw, call invalidate().

If you set a background drawable for a View, then the View will draw it before calling back to its onDraw() method

Event Handling and Threading

If either requestLayout() or invalidate() were called, the framework will take care of measuring, laying out, and drawing the tree as appropriate.

Focus Handling

Views indicate their willingness to take focus through the isFocusable method. To change whether a view can take focus, call setFocusable(boolean). When in touch mode (see notes below) views indicate whether they still would like focus via isFocusableInTouchMode and can change this via setFocusableInTouchMode(boolean).

To get a particular view to take focus, call requestFocus().

Touch Mode

For a touch capable device, once the user touches the screen, the device will enter touch mode. From this point onward, only views for which isFocusableInTouchMode is true will be focusable, such as text editing widgets. Other views that are touchable, like buttons, will not take focus when touched; they will only fire the on click listeners.

The touch mode state is maintained across android.app.Activitys. Call isInTouchMode to see whether the device is currently in touch mode.

Scrolling

scrollBy(int, int), scrollTo(int, int), awakenScrollBars()

Tags

<View ...
       android:tag="@string/mytag_value" />
<View ...>
       <tag android:id="@+id/mytag"
                android:value="@string/mytag_value" />
</View>

setTag(Object) or setTag(int, Object)

Themes

default, using the theme Context object supplied(the inflation context’s theme) , can also be specified by android:theme attr, or passing ContentThemeWrapper to constructor

the specified theme is applied on top of the inflation context’s theme (see LayoutInflater) and used for the view itself as well as any child elements.

Properties

setAlpha(float) TRANSLATION_X and TRANSLATION_Y

Animation

preferred to use android.animation package APIs. Animator-based is good.

pre-3.0 Animation-based, animate only the drawn on display. is not encouraged to use.

In particular, the ViewPropertyAnimator class makes animating these View properties particularly easy and efficient

Security

setFilterTouchesWhenObscured(boolean)

When enabled, the framework will discard touches that are received whenever the view’s window is obscured by another visible window. As a result, the view will not receive touches whenever a toast, dialog or other window appears above the view’s window.

onFilterTouchEventForSecurity(MotionEvent)

android.view.MotionEvent

public static final int FLAG_WINDOW_IS_OBSCURED = 0x1

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-09-30 12:02:39  更:2021-09-30 12:03:42 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 19:40:43-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码