接上上一个文章
三、Hello World 程序
1、res/layout/main.xml
2、res/values/strings.xml
3、res/drawable 目录下放置一个名为 icon.png 的图片文件
4、AndroidManifest.xml
5、Main.java
至此,我们完成了第一个Android程序的编写,开启我们Android开发之路。
三、Hello World 程序
1、res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<!–
设置 ID 的方式:ID前加前缀,@ id/
引用资源文件内字符串资源的方式:指定的资源名称前加前缀,@string/
–>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@ id/layout"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@ id/txt"
/>
</LinearLayout>
2、res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">hello</string> layout 直接调用 values 中的字符串
<string name="hello2">hello2</string> 编程方式调用 values 中的字符串
<string name="app_name">hello_app_name</string>
</resources>
3、res/drawable 目录下放置一个名为 icon.png 的图片文件
4、AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.flytosea.hello"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Main"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
5、Main.java
package com.flytosea.hello;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 将指定的布局文件作为 Activity 所显示的内容
setContentView(R.layout.main);
// 动态地在指定的容器控件上添加新的控件
TextView txt = new TextView(this);
txt.setText("测试文本");
// setContentView(txt);
((LinearLayout)this.findViewById(R.id.layout)).addView(txt);
// 引用资源文件内的内容作为输出内容
TextView txt1 = (TextView)this.findViewById(R.id.txt);
txt1.setText(this.getString(R.string.hello2));
}
}
至此,我们完成了第一个Android程序的编写,开启我们Android开发之路。
本文详细介绍了一个简单的Android应用程序的创建过程,包括布局文件的编写、资源文件的使用、AndroidManifest.xml的配置以及Java代码的实现。通过这个示例,读者可以了解如何在Android应用中展示文本并调用资源。
113

被折叠的 条评论
为什么被折叠?



