兩個佈局之間的跳轉
阿新 • • 發佈:2019-02-11
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部分程式碼如下:
建立的TimeActivity.java程式碼如下:<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的生命週期