Android 2.2发布了,我以前对移动开发不感兴趣的,由于工作原因,老是和这些东西打交道,于是乎,业余时间自己学点吧,程序员真辛苦。

怎 么搭建环境就不说了,直接从写程序开始。


import android.app.Activity;
import android.os.Bundle;

public class ShowPublication extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


}
}

这个就是简单的Activity

注意,这个class是继承Activity的,Activity是用来表现一个动作的单 个个体文件,每个Activity就是一个动作,一个应用程序可以有很多的Activity,但是用户每次只和其中之一进行交互,onCreate()这 个方法是在程序每次运行的时候就会被调用,这个方法里就是你初始化所有东西和你UI的地方。每个Activity并不要求一定要有用户接口,但是一般都会 有的。

好吧,现在让我们修改一下程序。

import android.app.Activity;
import android.os.Bundle;

public class ShowPublication extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);
}
}

Android的用户接口是从一个叫View的class那里继承下来的,View是一个用来画图的类,可以生成用户界面,比如图片,按钮这些东 西,在这里,TextView是一个文本类,用来显示文本的。

在这个改变中,我们在这个Activity结构里增加了一个TextView,TextView是可以用Android的Context作为参数 的,一个Context就是一个对系统处理,它提供了很多服务,比如查找资源,链接数据库等等。Activity一个Context的子类,而我们这个类 是继承的Activity,所以也是一个Context类,所以可以作为TextView的参数被传入。

TextView tv = new TextView(this);

下一步,我们用setText ()定义了Text的内容。

最后,为了要把它显示出来,我 们需要把这个TextView传递给setContentView()这个方法, 如果你不用这个方法的话,系统就会黑屏,什么都没有。

好了,运行一下吧,出现了。

但是如果你要是有很多UI的话,每次修改UI的内容,你都要修改源 代码,岂不是很痛苦,现在我们就说一下怎么用xml来定义。

上面我们完成的那个例子是 programmatic的UI layout,就是说你把你的UI直接定义在源代码里,如果你有很多UI,就会很麻烦,所以现在我们介绍另外一种方案,基于XML的UI Layout.

打 开main.xml

<TextView
xmlns:android ="http://schemas.android.com/apk/res/android"
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello"
/>



这个xml的结构就是用从View那里继承下来的类作为节点,这里我们只有一个节点,就是TextView.

解释一下里面各个attribute的用途,看下表



Attribute Meaning
xmlns:android This is an XML namespace declaration that tells the Android tools that you are going to refer to common attributes defined in the Android namespace. The outermost tag in every Android layout file must have this attribute.
android:id This attribute assigns a unique identifier to the TextView element. You can use the assigned ID to reference this View from your source code or from other XML resource declarations.
android:layout_width This attribute defines how much of the available width on the screen this View should consume. In this case, it's the only View so you want it to take up the entire screen, which is what a value of "fill_parent" means.
android:layout_height This is just like android:layout_width, except that it refers to available screen height.
android:text This sets the text that the TextView should display. In this example, you use a string resource instead of a hard-coded string value. The hello string is defined in the res/values/strings.xml file. This is the recommended practice for inserting strings to your application, because it makes the localization of your application to other languages graceful, without need to hard-code changes to the layout file. For more information, see Resources and Internationalization .
res/values/下面,


打开string.xml,你可以任意修改里面的内容(只要xml语法正确)

<?xml version="1.0" encoding ="utf-8"?>
<resources>
<string name ="hello">
Hello, Android! I am a string resource!
</string>
<string name ="app_name">
Hello, Android
</string>
</resources>


修改一下Activity类

import android.app.Activity;
import android.os.Bundle;

public class ShowPublication extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


}
}

注意,我们去掉了TextView,运行试试看,发现显示内容和string.xml里面的一样,对吧,这样修改不是容易多了么?

我 会在下一篇文章里介绍一下R这个类。

更多相关文章

  1. Android(安卓)ListView异步加载图片时图片顺序混乱解决办法
  2. Android(安卓)listView scroll 恢复滚动位置
  3. Android(安卓)json通信(解析)方法
  4. Binder使用示例
  5. Android(安卓)rom开发:webview崩溃问题Binary XML file line #103
  6. Android(安卓)网络框架初探
  7. Android(安卓)GridView的使用
  8. 浅入浅出Android(014):HTTP GET获取文本内容
  9. 学习Android闹钟源代码(三)-AlarmClock类分析(part2)

随机推荐

  1. Android:TextView文字跑马灯的效果实现
  2. android 抽屉式滑动demo
  3. 修改ScrollView滚动条样式
  4. android inputmanager中事件的传递流程
  5. Android实现打电话功能
  6. 完美解决android Studio打开报错 https:/
  7. Android编译过程详解
  8. android发送短信
  9. Android(安卓)默认全面屏适配方案
  10. Android抽屉式按钮实现