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: 两对宽高。
- 测量后期望的宽高: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().
- 在屏幕上显示出的实际宽高: 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
|