AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?>
<manifest 对应的是根元素
xmlns:android=http://schemas.android.com/apk/res/android 对应使用的是schema
package="org.lxh.demo" 表示程序所在的包名称
android:versionCode="1" 应用程序的版本号
android:versionName="1.0" > 显示给用户的名称 <uses-sdk android:minSdkVersion="10" /> 此为应用程序所对应的最低SDK版本 <application 配置所有的应用程序
android:icon="@drawable/ic_launcher" 使用的图标
android:label="@string/app_name" >
<activity 表示配置一个Activity程序,如果有需要可以编写多个此节点
android:name=".Hello" 对应的Activity程序的名称
android:label="@string/app_name" > 表示的是应用程序的提示信息,使用的是string.xml
<intent-filter> 表示过滤器
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest> 但是一般在基础学习的前半部分,此文件基本上不用太大的修改,而唯一修改最多的地方就是main.xml文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 表示布局管器的布局形式,此为线型布局xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 此布局管理器的屏幕宽度,现在为当前手机宽度
android:layout_height="fill_parent" 此布局管理器的屏幕高度,现在为当前手机高度
android:orientation="vertical" > 组件的排列方式,此为垂直排列 <TextView 此为文本显示组件,显示提示信息的
android:layout_width="fill_parent" 指的是此组件的宽度为屏幕的宽度 android:layout_height="wrap_content" 组件的高度为文字的高度
android:text="@string/hello" /> 组件的默认显示文字,此时为
string.xml </LinearLayout> 以后的所有组件都要在此进行配置,或者是通过程序代码完成。
Activity和布局文件之间的联系非常的紧密,即可以通过Activity取得组件(但是需要配置ID),也可以使用Activity通过程序动态生成组件。
例子:
<TextView
android:id="@+id/mytext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
快捷键: Alt + / --> 进行自动提示。 现在配置了新的组件,这个新组件存在了ID,而在以后的Activity程序之中会直接使用此组件进行操作,而且一旦定义了组件之后,所有的内容也会自动的在R.java文件中生成一个引用的ID.
使用findViewById()方法根据R.java中定义的ID的数字去取得相应的组件。 给组件设置值有两种方法(通过配置文件所完成的):
第一种方法: 在继承Activity类中
TextView view = (TextView)super.findViewById(R.id.mytext); // 取得TextView组件
view.setText(R.string.hello);
Button btn = (Button)super.findViewById(R.id.mybtn);
btn.setText(R.string.btn);
第二种方法: 在main.xml文件(组件的设置)中
<TextView
android:id="@+id/mytext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" /> <Button
android:id="@+id/mybtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn" /> 通过程序动态生成组件 (只仅仅在继承Activity的类中写以下代码)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // 所有组件竖直摆放
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
TextView text = new TextView(this);
text.setText(super.getString(R.string.hello));
Button btn = new Button(this);
btn.setText(super.getString(R.string.btn));
layout.addView(text);
layout.addView(btn);
super.setContentView(layout);
} 小结:
※Android项目由若干个Activity程序所组成,每一个Activity都是一个Java类;
※一个Android项目中所有用到的资源都保存在res文件夹之中;
※Android中的组件需要在布局管理器中进行配置,之后在Activity程序中可以使用findViewById()方法查找并进行控制;
※在布局管理器中定义的每一个组件都有其对应的操作类,用户可以直接实例化这些类中的对象进行组件的定义显示;
※标准的Android项目,所有的文字显示信息应该保存在strings.xml文件中保存。