如何定义Android View类说明学习

移动开发 Android
众所周知Android View类的语法与C++非常相似,所以其困难不在于语言本身,而在于熟悉.NET的可管理环境和对.NET框架的理解。

本篇文章讲解下大多用户想要了解到的Android View类的问题,其实这个Android View类是Android 中的一个超类,通常在编程过程中常把它定义为View和Android的XML内容View。

Android View类是Android的一个超类,这个类几乎包含了所有的屏幕类型。但它们之间有一些不同。每一个view都有一个用于绘画的画布。这个画布可以用来进行任意扩展。本文为了方便起见,只涉及到了两个主要的View类型:定义View和Android的XML内容View。

在上面的代码中,使用的是“Hello World” XML View,它是以非常自然的方式开始的。    如果我们查看一下新的Android工程。就会发现一个叫main.xml的文件。在这个文件中,通过一个简单的XML文件,描述了一个屏幕的布局。这个简单的xml文件的内容如下:

  1. <?xml version="1.0" encoding="utf-8"?> <RelativeLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android" 
  3. android:orientation="vertical" android:layout_width="fill_parent"
  4.  android:layout_height="fill_parent"  > 
  5. <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" 
  6. android:layout_centerHorizontal="true" 
  7. android:text="Press the center key to locate yourself"  /> </RelativeLayout>  

上面的内容的功能看起来非常明显。这个特殊文件定义了一个相关的布局,这就意味着通过一个元素到另一个元素的关系或是它们父元素的关系来描述。对于视图来说,有一些用于布局的方法。

但是在本文中只关注于上述的xml文件。    RealtiveLayout中包含了一个填充整个屏幕的文本框(也就是我们的LocateMe activity)。这个LocateMe activity在默认情况下是全屏的,因此,文本框将继承这个属性,并且文本框将在屏幕的左上角显示。另外,必须为这个XML文件设置一个

  1. <?xml version="1.0" encoding="utf-8"?> <RelativeLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android" 
  3. android:orientation="vertical" android:layout_width="fill_parent"
  4.  android:layout_height="fill_parent"  > 
  5. <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" 
  6. android:layout_centerHorizontal="true" 
  7. android:text="Press the center key to locate yourself"  /> </RelativeLayout>  

视图也可以被嵌套,但和J2ME不同,我们可以将定制的视图和Android团队发布的Widgets一起使用。在J2ME中,开发人员被迫选择GameCanvas和J2ME应用程序画布。这就意味着如果我们想要一个定制的效果,就必须在GameCanvas上重新设计我们所有的widget。Android还不仅仅是这些,视图类型也可以混合使用。

Android View类还带了一个widget库,这个类库包括了滚动条,文本实体,进度条以及其他很多控件。这些标准的widget可以被重载或被按着我们的习惯定制。现在让我们来进入我们的例子。

【编辑推荐】

  1. 新一代的Android手机介绍 深度剖析Android SDK版本介绍
  2.  
  3. 谷歌Android手机能否站稳中国
  4. PythonAndroid深度剖析Android应用程序的四种构造块
  5. 探秘与众不同的Android系统?
责任编辑:chenqingxiang 来源: csdn
相关推荐

2010-01-07 13:11:09

设置JSON

2011-08-25 16:20:33

Lua脚本变量

2010-01-07 16:09:04

学习JSON

2010-02-07 14:02:16

Android 界面

2016-11-16 21:55:55

源码分析自定义view androi

2016-12-26 15:25:59

Android自定义View

2016-04-12 10:07:55

AndroidViewList

2010-02-02 18:14:38

Python函数

2013-05-20 17:04:09

2017-03-02 13:33:19

Android自定义View

2010-03-05 15:11:49

Android开发类

2013-05-20 17:07:26

2010-01-28 13:27:12

C++类定义

2012-05-18 10:52:20

TitaniumAndroid模块自定义View模块

2013-05-20 17:33:44

Android游戏开发自定义View

2013-01-06 10:43:54

Android开发View特效

2009-12-18 13:34:09

Ruby metacl

2017-03-14 15:09:18

AndroidView圆形进度条

2010-02-07 11:22:17

Android OS

2010-01-18 17:07:52

C++类
点赞
收藏

51CTO技术栈公众号