Building Custom Components
Android offers a sophisticated and powerful componentized model for building your UI, based on the fundamental layout classes: View
and ViewGroup
. To start with, the platform includes a variety of prebuilt View and ViewGroup subclasses — called widgets and layouts, respectively — that you can use to construct your UI.
The Basic Approach
Here is a high level overview of what you need to know to get started in creating your own View components:
- Extend an existing
View
class or subclass with your own class. - Override some of the methods from the superclass. The superclass methods to override start with '
on
', for example,onDraw()
,onMeasure()
, andonKeyDown()
. This is similar to theon...
events inActivity
orListActivity
that you override for lifecycle and other functionality hooks. - Use your new extension class. Once completed, your new extension class can be used in place of the view upon which it was based.
Tip: Extension classes can be defined as inner classes inside the activities that use them. This is useful because it controls access to them but isn't necessary (perhaps you want to create a new public View for wider use in your application).
Fully Customized Components
Fully customized components can be used to create graphical components that appear however you wish. Perhaps a graphical VU meter that looks like an old analog gauge, or a sing-a-long text view where a bouncing ball moves along the words so you can sing along with a karaoke machine. Either way, you want something that the built-in components just won't do, no matter how you combine them.
Fortunately, you can easily create components that look and behave in any way you like, limited perhaps only by your imagination, the size of the screen, and the available processing power (remember that ultimately your application might have to run on something with significantly less power than your desktop workstation).
To create a fully customized component:
- The most generic view you can extend is, unsurprisingly,
View
, so you will usually start by extending this to create your new super component. - You can supply a constructor which can take attributes and parameters from the XML, and you can also consume your own such attributes and parameters (perhaps the color and range of the VU meter, or the width and damping of the needle, etc.)
- You will probably want to create your own event listeners, property accessors and modifiers, and possibly more sophisticated behavior in your component class as well.
- You will almost certainly want to override
onMeasure()
and are also likely to need to overrideonDraw()
if you want the component to show something. While both have default behavior, the defaultonDraw()
will do nothing, and the defaultonMeasure()
will always set a size of 100x100 — which is probably not what you want. - Other
on...
methods may also be overridden as required.
Extend onDraw()
and onMeasure()
The onDraw()
method delivers you a Canvas
upon which you can implement anything you want: 2D graphics, other standard or custom components, styled text, or anything else you can think of.
Note: This does not apply to 3D graphics. If you want to use 3D graphics, you must extend SurfaceView
instead of View, and draw from a separate thread. See the GLSurfaceViewActivity sample for details.
onMeasure()
is a little more involved. onMeasure()
is a critical piece of the rendering contract between your component and its container. onMeasure()
should be overridden to efficiently and accurately report the measurements of its contained parts. This is made slightly more complex by the requirements of limits from the parent (which are passed in to the onMeasure()
method) and by the requirement to call the setMeasuredDimension()
method with the measured width and height once they have been calculated. If you fail to call this method from an overridden onMeasure()
method, the result will be an exception at measurement time.
At a high level, implementing onMeasure()
looks something like this:
- The overridden
onMeasure()
method is called with width and height measure specifications (widthMeasureSpec
andheightMeasureSpec
parameters, both are integer codes representing dimensions) which should be treated as requirements for the restrictions on the width and height measurements you should produce. A full reference to the kind of restrictions these specifications can require can be found in the reference documentation underView.onMeasure(int, int)
(this reference documentation does a pretty good job of explaining the whole measurement operation as well). - Your component's
onMeasure()
method should calculate a measurement width and height which will be required to render the component. It should try to stay within the specifications passed in, although it can choose to exceed them (in this case, the parent can choose what to do, including clipping, scrolling, throwing an exception, or asking theonMeasure()
to try again, perhaps with different measurement specifications). - Once the width and height are calculated, the
setMeasuredDimension(int width, int height)
method must be called with the calculated measurements. Failure to do this will result in an exception being thrown.
相關文章
- Web Components系列(三) —— 建立 Custom ElementsWeb
- Web Components 系列(二)—— 關於 Custom ElementsWeb
- Arduino BuildingUI
- Custom
- Unknown custom element: <> -
- graphite custom functionsFunction
- Building an Automatically Scaling Web ApplicationUIWebAPP
- 1469C Building a FenceUI
- [Vue] Props: Custom ValidationVue
- CSS Custom Highlight APICSSAPI
- CF1316E Team BuildingUI
- Building OpenNI using a cross-compilerUIROSCompile
- 探索Web ComponentsWeb
- Cypress系列(63)- 使用 Custom Commands
- 【Lintcode】1025. Custom Sort String
- A lightweight, ultra-fast tool for building observability pipelinesASTUI
- [leetcode] 1642. Furthest Building You Can ReachLeetCodeUI
- 「 重磅 」React Server ComponentsReactServer
- Angular 2 Components CommunicateAngular
- Web Components 小欖Web
- 飛利浦案例研究: Building Connectivity with Kotlin MultiplatformUIKotlinPlatform
- WPF Custom control and display binding and specific data
- 什麼是 Spartacus Custom Config initializer?
- WPF custom control contains datagrid,listbox,imageAI
- winston log 庫如何建立 custom logger
- 堅定地使用 CSS Custom PropertiesCSS
- How to build your custom release bazel version?UI
- astro中建立web componentsASTWeb
- Microsoft Office 2003 Web ComponentsROSWeb
- visual components premium特色功能REM
- Web Components系列(一) —— 概述Web
- 【譯】Web Components簡介Web
- 【譯】容器元件 (Container Components)元件AI
- Web Components 原生元件 TrendsWeb元件
- [翻譯]Building WhatsApp Ui with Flutter Part 2 : The Chat ListUIAPPFlutter
- WPF ListBox ListBox.ItemTemplate DataTemplate Custom UserControl
- 原生 CSS Custom Highlight 終於來了~CSS
- Visual Components軟體特點
- 初識 Web Components 與 StencilWeb