搭建Android开发环境写第一个Hello World程序2

接上上一个文章

三、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开发之路。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值