当前位置: 代码迷 >> Android >> 筹建Android开发环境写第一个Hello World程序2
  详细解决方案

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

热度:20   发布时间:2016-05-01 16:37:01.0
搭建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开发之路。
  相关解决方案