Activity的生命週期
MainActivity
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//顯式啓動第二個界面
Intent i=new Intent(getApplicationContext(),SecondActivity.class);
startActivity(i);//getApplicationContext()也可寫成MainActivity.this;
}
});
Log.d("MyApplication", "運行到onCreate ");
}
@Override
protected void onStart() {
super.onStart();
Log.d("MyApplication", "運行到onStart ");
}
@Override
protected void onResume() {
super.onResume();
Log.d("MyApplication", "運行到onResume ");
}
@Override
protected void onPause() {
super.onPause();
Log.d("MyApplication", "運行到onPause ");
}
@Override
protected void onStop() {
super.onStop();
Log.d("MyApplication", "運行到onStop ");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("MyApplication", "運行到onRestart ");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("MyApplication", "運行到onDestroy ");
}
}
SecondActivity
public class SecondActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondlayout);
Log.d("MyApplication", "SecondActivity運行到onCreate ");
}
protected void onStart() {
super.onStart();
Log.d("MyApplication", "SecondActivity運行到onStart ");
}
@Override
protected void onResume() {
super.onResume();
Log.d("MyApplication", "SecondActivity運行到onResume ");
}
@Override
protected void onPause() {
super.onPause();
Log.d("MyApplication", "SecondActivity運行到onPause ");
}
@Override
protected void onStop() {
super.onStop();
Log.d("MyApplication", "SecondActivity運行到onStop ");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("MyApplication", "SecondActivity運行到onDestroy ");
}
}
運行:
點擊”啓動第二個界面“按鈕:
返回:
返回:
常用的五種佈局
LinearLayout——線性佈局
RelativeLayout——相對佈局
FrameLayout——幀佈局
TableLayout——表格佈局
AbsoluteLayout——絕對佈局
LinearLayout
(1)在LinearLayout中定義orientation(水平horizontal、垂直vertical)
(2)其中控件可以使用android:layout_marginXXX=”20dp”屬性(XXX:Top、Bottom、Left、Right)設置該控件距離上,下,左,右邊界的距離爲20dp
(3)android:layout_margin=”20dp”距離上下左右各20dp
(4)grivaty和layout_gravity的聯繫與區別:
px像素 :2560*1440 320*240
dp 一英寸除以160(px),手機不同,分辨率不同,dp不同
sp 文本大小跟dp一樣,用於文本大小
gravity(center、center_horizontal、center_vertical)
margin 間距( marginleft、marginTop)
界面佈局
第四個:(嵌套)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2">
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2">
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"/>
</LinearLayout>
</LinearLayout>
RelativeLayout的屬性
<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"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕1"/>//默認左上
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕2"
android:layout_marginLeft="70dp"/>//距離左邊70dp
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕3"
android:layout_alignParentBottom="true"/>//下部,默認左邊
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕4"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>//右下
</RelativeLayout>