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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

洛克Lee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值