Android布局显示,前端使用xml进行展示,后端使用Java代码进行控制 ,使用的类主要继承自Activity。
创建一个布局文件进行的动作有:
1.创建位于src包的.java文件
2.创建位于res/layout中的.xml文件
3.修改位于values中的strings.xml,添加对应的string
4.修改AndroidManifest.xml中的activity内容
例如:创建一个MainActivity布局,则在项目中进行的动作有:
1.src中生成MainActivity.class
2.res/layout中生成activity_main.xml
3.res/values的strings.xml中增加条目,默认增加
<string name="app_name">MyUI</string>
4.AndroidManifest.xml中增加条目
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
</activity>
代码如下:mainactivity.java
package com.example.myui;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i("MainActivity", "OnCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_time = (ImageView)findViewById(R.id.time);
iv_time.setOnClickListener(new ivTimeListener());
}
class ivTimeListener implements OnClickListener{
@Override
public void onClick(View v){
Intent intent_ivtime = new Intent();
intent_ivtime.setClass(MainActivity.this, TimeActivity.class);
MainActivity.this.startActivity(intent_ivtime);
}
}
}
xml部分代码如下:
<LinearLayout
android:id="@+id/mthirdLinear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@color/gray"
android:orientation="vertical" >
<ImageView
android:id="@+id/time"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@color/gray"
android:src="@drawable/time" >
</ImageView>
</LinearLayout>
package com.example.myui;
import android.app.Activity;
import android.os.Bundle;
public class TimeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_time);
}
}
xml代码如下:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.myui.TimeActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/time_detect"
android:gravity="center"
android:layout_gravity="center"
android:textSize="80sp" />
</RelativeLayout>
主要的跳转为:
Intent intent_ivtime = new Intent();
intent_ivtime.setClass(MainActivity.this, TimeActivity.class);
MainActivity.this.startActivity(intent_ivtime);
开启另一个activity的生命周期