活动周期
一个活动的生命周期是从oncreat开始依次经过如图所示的过程,最后结束于destroy()
当在一个活动中启动另一个活动时,如果第一个活动被全覆盖了(即不可见)则第一个活动处于stop()状态,并没有被销毁。当再次进入第一个活动时会经历onRestart()—–>onstart( )的过程。如果第一个活动没有被全覆盖,处于可见状态但不可操作。若再次进入第一个活动会经历onpause()—->onResume()的阶段。
注意:从onStart()–>running–>onpause( )这几个阶段都是可操作的,只不过onResume( )和OnPause()这两个阶段的时间比较短,很快就结束。在OnStart()—->OnStop( )是可见的。
当手机内存不足时会释放onPause()和OnStop()这两个阶段。如果running()阶段也被释放了,说明手机死机了。
Activity异常情况下的生命周期
由于内存不足或者其他原因Activity被杀死后再重新启动Activity的时候的生命周期是这样的:onPause(onsaveInstanceState这两个方法不分前后)—>onStop()–>onDestory—>onCreat—->onStart—>onRestoreInstanceState—->onResume();
在singleTask和singleTop的生命周期
如果不需要重新建立Activity的时候的生命周期:
onPause—>onNewIntent—–>onResume;
通过这个方法onNewIntent()可以取出当前请求的信息。
linearLayout布局
故名思议,就是线性布局,
orientation,margin,gravity,layout_gravity的解析
orientation为确定这个线性布局的方向:horizontal(水平方向)vertical(垂直方向)。
margin[ˈmɑ:dʒɪn]本意是边缘的意思,即其四周距离这个控件的距离一般用单位dp。一般有:layout_marginLeft,layout_marginRight,layout_marginBottom,layout_marginTop这几种。例如
android:layout_marginLeft="50dp"
表示左边的控件距离这个控件的距离为50dp。
注意
这个margin为在原有距离上加上这个距离,而不是总共的距离,
例如这个控件已经与左边的控件有10dp的距离了如果再执行: android:layout_marginLeft=”50dp”就是60dp的距离。
gravity写在linearlayout的配置中,表示整体的控件的排列位置。有:bottom,right,left,top等位置可以选择。
例如:
android:gravity="top"
表示所有的控件靠顶排列。
layout_gravity与gratity的用法差不多,只不过是配置的一个具体的控件中的,表示一个控件的位置排列的方式。
px,dp,sp
这是安卓中的单位,一般在界面中dp用的比较多,sp用在文本中。px表示像素。
当一英寸中的像素为160时 1dp=1px,若为320时1dp=2px。dp与像素不同,随手机的分辨率的不同而不同。
LinearLayout嵌套
如果用linearLayout在界面中制作出不同的界面效果,必须用嵌套。
例如要制作成这样的界面:
就需界面
代码如下:
<?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:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical"
>
<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:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
>
<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="2"/>
</LinearLayout>
</LinearLayout>
这里充分利用了嵌套与weight方法。
RelativeLayout布局
顾名思义为相对布局,既然是相对布局,就必须有一个对象作为参考物。
首先是相对于parent的:
alignparentleft,right,Bottom,Top。是相对布局的边的位置,例如
alignparentleft是在布局的左边的位置。
还有相对于布局中间的位置:
centerparent,centerVertical,centerHorital是在布局的中间,垂直的中间,水平的中间。
例如
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
相对于一个Id的操作为:
toleftof,torightof,above,below是相对于这个id的方向的位置。
而alignleft,alignright,alignBotton,alignTop是相对于id的边的位置的布局。
注意
1)在LinearLayout和RelativeLayout中有一个属性android:visibility=”“,如果为gone则表示这个控件既没有显示出来也不存在。如果为invisible则表示不可见但他的位置还在,visible表示可见。
2)在LinearLayout中如果方向设置为:android:orientation=”vertical”则控件只能在水平方向上进行center,light,rigth等操作,而不能在垂直方向上执行buttom,center等操作,但可以设置上下控件之间的距离。
Tablelayout
表格布局模型以行列的形式管理子控件,每一行为一个TableRow的对象,当然也可以是一个View的对象。TableRow可以添加子控件,每添加一个为一列。
TableLayout属性:
android:collapseColumns:将TableLayout里面指定的列隐藏,若有多列需要隐藏,请用逗号将需要隐藏的列序号隔开。
android:stretchColumns:设置指定的列为可伸展的列,以填满剩下的多余空白空间,若有多列需要设置为可伸展,请用逗号将需要伸展的列序号隔开。
android:shrinkColumns:设置指定的列为可收缩的列。当可收缩的列太宽(内容过多)不会被挤出屏幕。当需要设置多列为可收缩时,将列序号用逗号隔开。
列元素(Button)属性:(奇怪的是button 里面没有android:layout_column 和android:layout_span两个属性,写进去无反应,还不知道为什么)
android:layout_colum:设置该控件在TableRow中指定的列。
android:layout_span:设置该控件所跨越的列数。
图片:
代码:
复制代码
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical"
6 tools:context=".AndroidTableLayoutActivity" >
7
8 <!-- 定义第一个表格,指定第2列允许收缩,第3列允许拉伸 -->
9
10 <TableLayout
11 android:id="@+id/tablelayout01"
12 android:layout_width="match_parent"
13 android:layout_height="wrap_content"
14 android:shrinkColumns="1"
15 android:stretchColumns="2" >
16
17 <!-- 直接添加按钮,自己占用一行 -->
18
19 <Button
20 android:id="@+id/btn01"
21 android:layout_width="wrap_content"
22 android:layout_height="wrap_content"
23 android:text="独自一行" >
24 </Button>
25
26 <TableRow>
27
28 <Button
29 android:id="@+id/btn02"
30 android:layout_width="wrap_content"
31 android:layout_height="wrap_content"
32 android:text="普通" >
33 </Button>
34
35 <Button
36 android:id="@+id/btn03"
37 android:layout_width="wrap_content"
38 android:layout_height="wrap_content"
39 android:text="允许被收缩允许被收缩允许被收缩允许被收缩" >
40 </Button>
41
42 <Button
43 android:id="@+id/btn04"
44 android:layout_width="wrap_content"
45 android:layout_height="wrap_content"
46 android:text="允许被拉伸" >
47 </Button>
48 </TableRow>
49 </TableLayout>
50 <!-- 定义第2个表格,指定第2列隐藏 -->
51
52 <TableLayout
53 android:id="@+id/tablelayout02"
54 android:layout_width="match_parent"
55 android:layout_height="wrap_content"
56 android:collapseColumns="1" >
57
58 <TableRow>
59
60 <Button
61 android:id="@+id/btn05"
62 android:layout_width="wrap_content"
63 android:layout_height="wrap_content"
64 android:text="普通" >
65 </Button>
66
67 <Button
68 android:id="@+id/btn06"
69 android:layout_width="wrap_content"
70 android:layout_height="wrap_content"
71 android:text="被隐藏列" >
72 </Button>
73
74 <Button
75 android:id="@+id/btn07"
76 android:layout_width="wrap_content"
77 android:layout_height="wrap_content"
78 android:text="允许被拉伸" >
79 </Button>
80 </TableRow>
81 </TableLayout>
82 <!-- 定义第3个表格,指定第2列填满空白-->
83
84 <TableLayout
85 android:id="@+id/tablelayout03"
86 android:layout_width="match_parent"
87 android:layout_height="wrap_content"
88 android:stretchColumns="1"
89 >
90
91 <TableRow>
92
93 <Button
94 android:id="@+id/btn08"
95 android:layout_width="wrap_content"
96 android:layout_height="wrap_content"
97 android:text="普通" >
98 </Button>
99
100 <Button
101 android:id="@+id/btn09"
102 android:layout_width="wrap_content"
103 android:layout_height="wrap_content"
104 android:text="填满剩余空白" >
105 </Button>
106 </TableRow>
107 </TableLayout>
108 <!-- 定义第3个表格,指定第2列横跨2列-->
109
110 <TableLayout
111 android:id="@+id/tablelayout04"
112 android:layout_width="match_parent"
113 android:layout_height="wrap_content"
114 >
115
116 <TableRow>
117
118 <Button
119 android:id="@+id/btn10"
120 android:layout_width="wrap_content"
121 android:layout_height="wrap_content"
122 android:text="普通" >
123 </Button>
124
125 <Button
126 android:id="@+id/btn11"
127 android:layout_column="2"
128 android:layout_width="wrap_content"
129 android:layout_height="wrap_content"
130 android:text="填满剩余空白" >
131 </Button>
132 </TableRow>
133 </TableLayout>
134 </LinearLayout>