搭建好android 开发环境后,开始开发第一个程序hello world,在网上找了很多的例子,都是用自动的代码生成器。自己查了一些资料,完成了hello world的两种UI写法,一种是xml定义出来。一种是用代码实现,在用代码实现的时候发现,跟SWT开发比较相近.
一) XML 形式
在res/values/strings.xml 中定义资源文件 <string name="hellowzg">my first android app is hellowzg</string>
保存之后,会在R.java 中生成
public static final class string {
public static final int app_name=0x7f040000;
public static final int hellowzg=0x7f040001;
}
在res/layout 中新建一个文件 hellowzg_main.xml 用来定义页面的UI布局,在定义好文件之后,保存,插件会自动在R.java中生成
public static final class layout {
public static final int hellowzg_main=0x7f030000;
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hellowzg"
tools:context=".MainActivity"/>
</RelativeLayout>
在src 下 新建com.example.myandroid.Hellowzg 类 继承Activity类,实现 onCreate 方法
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hellowzg_main);
}
更改 AndroidManifest.xml. 其中应用的name是制定其启动时候调用的应用入口,其中默认的当前路径是package中定义的,尝试了一下更改了hellowzg类的路径,如果是用全路径也可以执行成功
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myandroid"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="15" />
<application android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme">
<activity
android:name=".Hellowzg"
android:label="@string/hellowzg" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
二)用代码实现
感觉用代码实现的话,其很想SWT的开发。实际上就跟配置xml是一样的,可以对应来看。
不用在res中定义hellowzg_main.xml 了,直接在com.example.myandroid.Hellowzg 中修改onCreate 方法,不用setContentView(R.layout.hellowzg_main);来放置定义的UI了,而是按照java的形式来实现。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"></<RelativeLayout>
转为java 代码的形式就是
RelativeLayout relativeLayout = new RelativeLayout(this);
LayoutParams layoutParam = new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(layoutParam);
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hellowzg"
tools:context=".MainActivity"/>
转为java代码的形式:
TextView text1 = new TextView(this);
text1.setText("hello world!");
LayoutParams layoutParam2 = new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParam2.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
layoutParam2.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
text1.setLayoutParams(layoutParam2);
本来以为 android:layout_centerHorizontal="true" 这种形式的,应该是 TextView 的属性,或者是LayoutParams 中定义好的属性,直接调用方法,或者像SWT 中用FORMLayout那样操作的方式,查询了资料,发现可以用addRule来操作.
然后
relativeLayout.addView(text1);
setContentView(relativeLayout);
就可以了
android 第一个程序hello world 两种实现
猜你喜欢
转载自zhigangwangxx.iteye.com/blog/1705906
今日推荐
周排行