Android-View
发布日期:2022-03-18 08:27:31 浏览次数:40 分类:技术文章

本文共 5417 字,大约阅读时间需要 18 分钟。

请参考:

Using Views

  • 调用requestFocus()来Set focus。
  • setVisibility(int)来设置可见性。

Implementing a Custom View

自定义View可能需要被重写的方法有:

  • onMeasure(int, int):调用该方法来检测View组件及它所包含的所有子组件的大小。
  • onLayout(boolean, int, int, int, int):当该组件需要分配其子组件的位置、大小时,该方法就会被回调。
  • onSizeChanged(int, int, int, int):当该组件的大小被改变时回调该方法。
  • onDraw(android.graphics.Canvas):当该组件将要绘制它的内容时回调该方法进行绘制。
  • onTouchEvent(MotionEvent):当发生触摸屏事件时触发该方法。

Position位置

一个View具有一个位置,表示为一对相对左边和顶部的坐标,和两个尺寸,高度和宽度。

可以通过getLeft()和getTop()来获取view的位置。也有一些便捷的方法,来避免不必要的计算,例如getRight()和getBottom()。getRight()就相当于getLeft() + getWidth() 。

Size, padding and margins

一个View的宽度和高度,实际上有2对宽度和高度值。

一对被成为measured width和measured height。这些尺寸定义的是view在其parent内的大小。可以通过getMeasuredWidth() 和 getMeasuredHeight()来获取。
另一对可以简单的被称为width和height,有时也被成为drawing width和drawing height。这些尺寸定义的是在绘制时和布局后的,屏幕上的view的实际尺寸。这些值可以,但不是必要的,与measured width 和 height不同。可以通过调用getWidth()和getHeight()来获取。
一个View测量其尺寸时,要考虑到padding。padding是view的左,上,右和下边的像素。Padding can be used to offset the content of the view by a specific amount of pixels。可以通过使用setPadding(int, int, int, int)或者setPaddingRelative(int, int, int, int)方法来设置Padding。可用调用getPaddingLeft(), getPaddingTop(), getPaddingRight(), getPaddingBottom(), getPaddingStart(), getPaddingEnd()来获取Padding。
虽然一个View可以定义一个padding,但是它不支持margins。然而,view groups提供。请参考了解更多消息。

Layout布局

Layout is a two pass process: a measure pass and a layout pass. The measuring pass is implemented in and is a top-down traversal of the view tree. Each view pushes dimension specifications down the tree during the recursion. At the end of the measure pass, every view has stored its measurements. The second pass happens in and is also top-down. During this pass each parent is responsible for positioning all of its children using the sizes computed in the measure pass.

When a view’s measure() method returns, its and values must be set, along with those for all of that view’s descendants. A view’s measured width and measured height values must respect the constraints imposed by the view’s parents. This guarantees that at the end of the measure pass, all parents accept all of their children’s measurements. A parent view may call measure() more than once on its children. For example, the parent may measure each child once with unspecified dimensions to find out how big they want to be, then call measure() on them again with actual numbers if the sum of all the children’s unconstrained sizes is too big or too small.

The measure pass uses two classes to communicate dimensions. The class is used by views to tell their parents how they want to be measured and positioned. The base LayoutParams class just describes how big the view wants to be for both width and height. For each dimension, it can specify one of:

  • 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).

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.
To intiate a layout, call requestLayout(). This method is typically called by a view on itself when it believes that is can no longer fit within its current bounds.

Drawing

Drawing is handled by walking the tree and recording the drawing commands of any View that needs to update. After this, the drawing commands of the entire tree are issued to screen, clipped to the newly damaged area.

The tree is largely recorded and drawn in order, with parents drawn before (i.e., behind) their children, with siblings drawn in the order they appear in the tree. If you set a background drawable for a View, then the View will draw it before calling back to its onDraw() method. The child drawing order can be overridden with in a ViewGroup, and with custom Z values} set on Views.

To force a view to draw, call .

Event Handling and Threading

The basic cycle of a view is as follows:

  1. An event comes in and is dispatched to the appropriate view. The view handles the event and notifies any listeners.
  2. If in the course of processing the event, the view’s bounds may need to be changed, the view will call requestLayout().
  3. Similarly, if in the course of processing the event the view’s appearance may need to be changed, the view will call invalidate().
  4. If either requestLayout() or invalidate() were called, the framework will take care of measuring, laying out, and drawing the tree as appropriate.

Note: The entire view tree is single threaded. You must always be on the UI thread when calling any method on any view. If you are doing work on other threads and want to update the state of a view from that thread, you should use a .

其它

View的Margin与Padding的区别

这里写图片描述

View的LifeCycle

这里写图片描述

转载地址:https://windzen.blog.csdn.net/article/details/46483357 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Interface Builder操作
下一篇:Xcode应用设置打包相关

发表评论

最新留言

很好
[***.229.124.182]2024年03月30日 03时11分52秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章