1 AndroidManifest.xml
2
3 <?xml version="1.0" encoding="utf-8"?>
4 <manifest 对应的是根元素
5 xmlns:android=http://schemas.android.com/apk/res/android 对应使用的是schema
6 package="org.lxh.demo" 表示程序所在的包名称
7 android:versionCode="1" 应用程序的版本号
8 android:versionName="1.0" > 显示给用户的名称
9
10 <uses-sdk android:minSdkVersion="10" /> 此为应用程序所对应的最低SDK版本
11
12 <application 配置所有的应用程序
13 android:icon="@drawable/ic_launcher" 使用的图标
14 android:label="@string/app_name" >
15 <activity 表示配置一个Activity程序,如果有需要可以编写多个此节点
16 android:name=".Hello" 对应的Activity程序的名称
17 android:label="@string/app_name" > 表示的是应用程序的提示信息,使用的是string.xml
18 <intent-filter> 表示过滤器
19 <action android:name="android.intent.action.MAIN" />
20
21 <category android:name="android.intent.category.LAUNCHER" />
22 </intent-filter>
23 </activity>
24 </application>
25
26 </manifest>
27
28
29 但是一般在基础学习的前半部分,此文件基本上不用太大的修改,而唯一修改最多的地方就是main.xml文件。
30 <?xml version="1.0" encoding="utf-8"?>
31 <LinearLayout 表示布局管器的布局形式,此为线型布局xmlns:android="http://schemas.android.com/apk/res/android"
32 android:layout_width="fill_parent" 此布局管理器的屏幕宽度,现在为当前手机宽度
33 android:layout_height="fill_parent" 此布局管理器的屏幕高度,现在为当前手机高度
34 android:orientation="vertical" > 组件的排列方式,此为垂直排列
35
36
37 <TextView 此为文本显示组件,显示提示信息的
38 android:layout_width="fill_parent" 指的是此组件的宽度为屏幕的宽度 android:layout_height="wrap_content" 组件的高度为文字的高度
39 android:text="@string/hello" /> 组件的默认显示文字,此时为
40 string.xml
41
42 </LinearLayout>
43
44 以后的所有组件都要在此进行配置,或者是通过程序代码完成。
45 Activity和布局文件之间的联系非常的紧密,即可以通过Activity取得组件(但是需要配置ID),也可以使用Activity通过程序动态生成组件。
46 例子:
47 <TextView
48 android:id="@+id/mytext"
49 android:layout_width="fill_parent"
50 android:layout_height="wrap_content"
51 android:text="@string/hello" />
52 快捷键: Alt + / --> 进行自动提示。
53
54 现在配置了新的组件,这个新组件存在了ID,而在以后的Activity程序之中会直接使用此组件进行操作,而且一旦定义了组件之后,所有的内容也会自动的在R.java文件中生成一个引用的ID.
55 使用findViewById()方法根据R.java中定义的ID的数字去取得相应的组件。
56
57 给组件设置值有两种方法(通过配置文件所完成的):
58 第一种方法: 在继承Activity类中
59 TextView view = (TextView)super.findViewById(R.id.mytext); // 取得TextView组件
60 view.setText(R.string.hello);
61 Button btn = (Button)super.findViewById(R.id.mybtn);
62 btn.setText(R.string.btn);
63 第二种方法: 在main.xml文件(组件的设置)中
64 <TextView
65 android:id="@+id/mytext"
66 android:layout_width="fill_parent"
67 android:layout_height="wrap_content"
68 android:text="@string/hello" />
69
70 <Button
71 android:id="@+id/mybtn"
72 android:layout_width="fill_parent"
73 android:layout_height="wrap_content"
74 android:text="@string/btn" />
75
76
77 通过程序动态生成组件 (只仅仅在继承Activity的类中写以下代码)
78 public void onCreate(Bundle savedInstanceState) {
79 super.onCreate(savedInstanceState); // 所有组件竖直摆放
80 LinearLayout layout = new LinearLayout(this);
81 layout.setOrientation(LinearLayout.VERTICAL);
82 TextView text = new TextView(this);
83 text.setText(super.getString(R.string.hello));
84 Button btn = new Button(this);
85 btn.setText(super.getString(R.string.btn));
86 layout.addView(text);
87 layout.addView(btn);
88 super.setContentView(layout);
89 }
90
91 小结:
92 ※Android项目由若干个Activity程序所组成,每一个Activity都是一个Java类;
93 ※一个Android项目中所有用到的资源都保存在res文件夹之中;
94 ※Android中的组件需要在布局管理器中进行配置,之后在Activity程序中可以使用findViewById()方法查找并进行控制;
95 ※在布局管理器中定义的每一个组件都有其对应的操作类,用户可以直接实例化这些类中的对象进行组件的定义显示;
96 ※标准的Android项目,所有的文字显示信息应该保存在strings.xml文件中保存。