Android原理揭秘系列之View、ViewGroup

作过Android 应用开发的朋友都知道,Android的UI界面都是由View和ViewGroup及其派生类组合而成的。其中,View是所有UI组件的基类,而ViewGroup是容纳这些组件的容器,其本身也是从View派生出来的。AndroidUI界面的一般结构可参见下面的示意图:

可见,作为容器的ViewGroup可以包含作为叶子节点的View,也可以包含作为更低层次的子ViewGroup,而子ViewGroup又可以包含下一层的叶子节点的View和ViewGroup。事实上,这种灵活的View层次结构可以形成非常复杂的UI布局,开发者可据此设计、开发非常精致的UI界面。

一般来说,开发Android应用程序的UI界面都不会直接实用View和ViewGroup,而是使用这两大基类的派生类。

View派生出的直接子类有:AnalogClock,,ViewStub

View派生出的间接子类有:AbsListView,AbsSeekBar, AbsSpinner, AbsoluteLayout, AdapterView<TextendsAdapter>,AdapterViewAnimator, AdapterViewFlipper, AppWidgetHostView, AutoCompleteTextView,CalendarView, CheckBox, CheckedTextView, Chronometer, CompoundButton,

ViewGroup派生出的直接子类有:,SlidingDrawer

ViewGroup派生出的间接子类有:AbsListView,AbsSpinner, AdapterViewAnimator, AdapterViewFlipper, AppWidgetHostView, CalendarView, DatePicker, DialerFilter, ExpandableListView, Gallery, GestureOverlayView,GridView,HorizontalScrollView, ImageSwitcher,ListView,

上面View、ViewGroup的直接子类和间接别子类中标记为红色的类是我们再应用开发中接触和用得比较频繁的类,需要初学者重点熟悉和掌握,其详细的API及用法可参见SDK的说明。这里特别指出,ImageView是布局具有图片效果的UI常用的类,SurfaceView是用来进行游戏开发的与一般View相比较为特殊的非常重要的类(后续会对原理作深入分析),而AbsoluteLayout、FrameLayout,LinearLayout, RelativeLayout这几个ViewGroup的直接子类是Android UI布局中最基本的布局元素。

值得一提的是,上述的所有基类、派生类都是Android framework层集成的标准系统类,开发者在应用开发中可直接引用SDK中这些系统类及其API。但事实上,在UI开发的很多场景下,直接使用这些系统类并不能满足应用开发的需要。比如说,我们想用ImageView在默认情况下加载一幅图片,但是希望在点击该View时View变换出各种图像处理效果,这个时候直接使用ImageView是不行的,此时我们可以重载ImageView,在新派生出的子控件中重载OnDraw等方法来实现我们的定制效果。这种派生出系统类的子类方法我们通常称之为自定义控件。自定义控件可像标准View控件那样在XML及我们的Java文件中进行布局和实例化,但在布局时必须指出其完整的包名和类名。事实上,自定义控件的使用是我们进行Android 开发的必不可少的基本用法,是必须掌握的基本技巧。

下面我们来看一个典型的例子,我们待机Launcher 的XML布局,这个例子能够很好的说明上面的一些概念。

<?xml version="1.0" encoding="utf-8"?><!– Copyright (C) 2007 The Android Open Source ProjectLicensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License atUnless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.–><com.android.launcher2.DragLayerxmlns:android=""xmlns:launcher=""android:id="@+id/drag_layer"android:layout_width="match_parent"android:layout_height="match_parent"><include layout="@layout/all_apps" /><!– The workspace contains 3 screens of cells –><com.android.launcher2.Workspaceandroid:id="@+id/workspace"android:layout_width="match_parent"android:layout_height="match_parent"launcher:defaultScreen="2"><include android:id="@+id/cell1" layout="@layout/workspace_screen" /><include android:id="@+id/cell2" layout="@layout/workspace_screen" /><include android:id="@+id/cell3" layout="@layout/workspace_screen" /><include android:id="@+id/cell4" layout="@layout/workspace_screen" /><include android:id="@+id/cell5" layout="@layout/workspace_screen" /></com.android.launcher2.Workspace><ImageViewandroid:id="@+id/previous_screen"android:layout_width="93dip"android:layout_height="@dimen/button_bar_height"android:layout_gravity="bottom|left"android:layout_marginLeft="6dip"android:scaleType="center"android:src="@drawable/home_arrows_left"android:onClick="previousScreen"android:focusable="true"android:clickable="true" /><ImageViewandroid:id="@+id/next_screen"android:layout_width="93dip"android:layout_height="@dimen/button_bar_height"android:layout_gravity="bottom|right"android:layout_marginRight="6dip"android:scaleType="center"android:src="@drawable/home_arrows_right"android:onClick="nextScreen"android:focusable="true"android:clickable="true" /><com.android.launcher2.DeleteZoneandroid:id="@+id/delete_zone"android:layout_width="@dimen/delete_zone_size"android:layout_height="@dimen/delete_zone_size"android:paddingTop="@dimen/delete_zone_padding"android:layout_gravity="bottom|center_horizontal"android:scaleType="center"android:src="@drawable/delete_zone_selector"android:visibility="invisible"launcher:direction="horizontal"/><RelativeLayoutandroid:id="@+id/all_apps_button_cluster"android:layout_width="fill_parent"android:layout_height="@dimen/button_bar_height"android:layout_gravity="bottom|center_horizontal"android:paddingTop="2dip"><com.android.launcher2.HandleViewstyle="@style/HotseatButton"android:id="@+id/all_apps_button"android:layout_centerHorizontal="true"android:layout_alignParentBottom="true"android:src="@drawable/all_apps_button"launcher:direction="horizontal"/><ImageViewandroid:id="@+id/hotseat_left"style="@style/HotseatButton.Left"android:layout_toLeftOf="@id/all_apps_button"android:src="@drawable/hotseat_phone"android:onClick="launchHotSeat"/><ImageViewandroid:id="@+id/hotseat_right"style="@style/HotseatButton.Right"android:layout_toRightOf="@id/all_apps_button"android:src="@drawable/hotseat_browser"android:onClick="launchHotSeat"/></RelativeLayout></com.android.launcher2.DragLayer>到尽头,也许快乐,或有时孤独,如果心在远方,

Android原理揭秘系列之View、ViewGroup

相关文章:

你感兴趣的文章:

标签云: