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>