Android开发权威指南(SDK2.3)笔记
第4章 我的UI我作主————用户界面开发基础
在不同Activity之间传递数据
4种方法:
- 使用Intent。
intent.putExtra(key, value)
。在接收方:getIntent().getStringExtra(key)
,getIntent().getInt(key)
- 使用静态变量。在Activity或其子类中定义
public static
的静态变量,在startActivity
之前为这些变量赋值。官方不推荐 - 使用剪切板。
ClipboardManager clipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
,clipboard.setText(string);
。缺点:只能传递字符串。(注:可通过ByteArrayOutputStream
,ObjectOutputStream
,Base64
将可序列化的对象转换为字符串,接收方再转换为原对象来实现传递。 - 使用全局对象。全局对象对应的类必须是android.app.Application的子类。全局类不需要定义静态变量,定义成public成员变量就可以。全局类必须有无参的构造方法(因为它是由系统调用的)。在代码中通过
getApplicationContext()
获得全局对象,并可对其属性赋值。
视图:布局
几个重要的xml属性:
- android:layout_gravity:当前视图在父视图中的位置。
- android:gravity:表示容器中的子视图在容器中的位置。
- android:layout_marginTop:当前视图边缘离基线的距离。例如,当前的
是居中的,那么这条基线就是屏幕的中位线,所以80dp就是 顶端到这条基线的距离。小鸟的图像会在基线的下面的位置显示。
重用xml布局文件
使用<include>
标签:
<include android:id="@+id/cell1" layout=@layout/workspace_screen" />
<include android:id="@+id/cell2" layout=@layout/workspace_screen" />
<include android:id="@+id/cell3" layout=@layout/workspace_screen" />
注:如果要覆盖布局的尺寸,必须同时覆盖android:layout_width
(原书为android:layout_weight)和android:layout_height
。只覆盖一个无效。
Written with StackEdit.
No comments:
Post a Comment