1. activity常用方法
activity可以看作是一个应用程序或者任务或者进程。由于手机资源的限制,很多手机操作系统虽然支持多任务机制,但往往只支持一个任务在前台操作。即用户只能看到一个程序在前台运行,并占据了整个屏幕,如果需要切换到其它任务,需要先把当前的任务隐藏(切换到后台)。Android也是这样。
1. onCreate
在该函数中可以做一些初始化或者对象创建等工作。只在activity创建时调用一次。
2. setContentView
用于显示某xml描述的view。
3. startActivity
启动另外一个activity。
2. xml布局
最常用的为LinearLayout、RelativeLayout布局。
LinearLayout:可以为水平或者垂直线性布局,在一个方向上对齐所有子元素。
例如:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<CheckBox android:id="@+id/cbx_audio"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/cbx_audio_txt"
android:textSize="20dp"/>
<CheckBox android:id="@+id/cbx_virtualpad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/cbx_virtualpad_txt"
android:textSize="20dp"/>
<TextView android:id="@+id/multitouch"
android:textSize="15dp"
android:paddingLeft="20dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
…
</LinearLayout>
实现的效果为:
RelativeLayout:可以指定子元素相对于其它元素或者父布局的位置。
<RelativeLayout
android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:text="@string/btn_up_txt"
android:id="@+id/btn_up"
android:textSize="20dp"
android:gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/up_dir"/>
<EditText android:id="@+id/tv_path"
android:textSize="20dp"
android:text="/sdcard"
android:focusable="false"
android:layout_toRightOf="@+id/btn_up"
android:layout_marginLeft="2dip"
android:layout_centerVertical="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>实现的效果为: