View 翻译

本文深入探讨了Android应用中视图组件的基本概念、如何使用视图构建交互式用户界面,以及如何定制视图以实现特定功能。重点介绍了视图与视图组的层级关系、属性设置、焦点管理、事件监听、可见性控制等关键操作,同时提供了自定义视图的方法和步骤。此外,文章还简要概述了视图在界面上的布局与绘制过程,为开发者提供了一站式的用户界面开发指南。

http://developer.android.com/intl/zh-CN/reference/android/view/View.html


类概述(Class Overview)


这个类表示用户界面组件的基本构建块。视图占据了屏幕上的一个矩形区域,并负责绘图和事件处理。View是基类的部件,这是用来创建交互式UI组件(按钮,文本框等)。ViewGroup子类是基类的布局,这是invisible的容器,用来装别的View或ViewGroup,并定义它们的布局属性。

开发指南

如需使用这个类来开发应用程序的用户界面的信息,阅读的用户界面开发指南。

使用视图

所有的View,在一个窗口中排列像“一棵树”。您可以添加代码或通过指定树中的一个或多个XML布局文件。有许多专门的子类的View

,作为控制或能够显示文字,图像或其他内容。(树: 在对界面进行布局是就像一颗树。从根元素开始布局。)

一旦你已经创建了一个树的View,一般有几类常见的操作,你不妨来执行:

  • Set properties:例如设置一个文本TextView。可用的属性和设置的方法,他们将各不相同意见不同的子类。请注意,在编译时已知的属性可以设置在XML布局文件。
  • 设置焦点(Set focus):框架将处理移动响应用户输入的焦点。要强制焦点到一个特定的视图,调用requestFocus()
  • Set up listeners:Views allow clients to set listeners that will be notified when something interesting happens to the view。例如,所有的View,将让你设置一个监听的观点获得或失去焦点时通知。您可以寄存器使用setOnFocusChangeListener(android.view.View.OnFocusChangeListener)这样一个监听器 。其他视图子类提供更专业的监听器。例如,一个按钮 一个监听器按钮被点击时通知客户。
  • 设置可见性:您可以隐藏或显示使用setVisibility(int)

Note: The Android framework is responsible for measuring, laying out and drawing views. You should not call methods that perform these actions on views yourself unless you are actually implementing aViewGroup.

自定义视图

要实现之定义的View ,你通常要overrides 有些方法 ,这个framework 将要回调所有的views。你不需要覆盖所有这些方法。事实上,你就可以开始只是重写 OnDraw(android.graphics.Canvas)








Category Methods Description
构造 Constructors There is a form of the constructor that are called when the view is created from code and a form that is called when the view is inflated from a layout file. The second form should parse and apply any attributes defined in the layout file.
onFinishInflate() Called after a view and all of its children has been inflated from XML.
布局t onMeasure(int, int) Called to determine the size requirements for this view and all of its children.
onLayout(boolean, int, int, int, int) Called when this view should assign a size and position to all of its children.
onSizeChanged(int, int, int, int) Called when the size of this view has changed.
onDraw(android.graphics.Canvas) Called when the view should render its content.
事件处理 onKeyDown(int, KeyEvent) Called when a new hardware key event occurs.
onKeyUp(int, KeyEvent) Called when a hardware key up event occurs.
onTrackballEvent(MotionEvent) Called when a trackball motion event occurs.
onTouchEvent(MotionEvent) Called when a touch screen motion event occurs.
Focus onFocusChanged(boolean, int, android.graphics.Rect) Called when the view gains or loses focus.
onWindowFocusChanged(boolean) Called when the window containing the view gains or loses focus.
Attaching onAttachedToWindow() Called when the view is attached to a window.
onDetachedFromWindow() Called when the view is detached from its window.
onWindowVisibilityChanged(int) Called when the visibility of the window containing the view has changed.





评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值