第一类:属性值为true或false
android:layout_centerhrizontal 水平居中
android:layout_centervertical 垂直居中
android:layout_centerinparent 相对于父元素完全居中
android:layout_alignparentbottom 贴紧父元素的下边缘
android:layout_alignparentleft 贴紧父元素的左边缘
android:layout_alignparentright 贴紧父元素的右边缘
android:layout_alignparenttop 贴紧父元素的上边缘
android:layout_alignwithparentifmissing 如果对应的兄弟元素找不到的话就以父元素做参照物
第二类:属性值必须为id的引用名“@id/id-name”
android:layout_below 在某元素的下方
android:layout_above 在某元素的的上方
android:layout_toleftof 在某元素的左边
android:layout_torightof 在某元素的右边
android:layout_aligntop 本元素的上边缘和某元素的的上边缘对齐
android:layout_alignleft 本元素的左边缘和某元素的的左边缘对齐
android:layout_alignbottom 本元素的下边缘和某元素的的下边缘对齐
android:layout_alignright 本元素的右边缘和某元素的的右边缘对齐
第三类:属性值为具体的像素值,如30dip,40px
android:layout_marginbottom 离某元素底边缘的距离
android:layout_marginleft 离某元素左边缘的距离
android:layout_marginright 离某元素右边缘的距离
android:layout_margintop 离某元素上边缘的距离
1. MainActivity.java
package powerise.demo01; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OptionalDataException; import java.io.StreamCorruptedException; import java.util.Date; import org.apache.commons.codec.binary.Base64; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { private EditText et_id = null; private EditText et_name = null; private Button btn_save = null; private TextView tv_result = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initGUIElements(); getObjectInfo(); btn_save.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String id = et_id.getText().toString(); String name = et_name.getText().toString(); saveObject(id, name); getObjectInfo(); } }); } protected void getObjectInfo() { try { SharedPreferences mSharedPreferences = getSharedPreferences("base64", Context.MODE_PRIVATE); String personBase64 = mSharedPreferences.getString("person", ""); byte[] base64Bytes = Base64.decodeBase64(personBase64.getBytes()); ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes); ObjectInputStream ois = new ObjectInputStream(bais); Person person = (Person) ois.readObject(); tv_result.setText(person.toString()); } catch (Exception e) { e.printStackTrace(); } } private void initGUIElements() { et_id = (EditText) findViewById(R.id.et_id); et_name = (EditText) findViewById(R.id.et_name); btn_save = (Button) findViewById(R.id.btn_save); tv_result = (TextView) findViewById(R.id.tv_result); } private void saveObject(String id, String name) { SharedPreferences mSharedPreferences = getSharedPreferences("base64", Context.MODE_PRIVATE); Person person = new Person(Integer.parseInt(id), name, new Date()); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(person); String personBase64 = new String(Base64.encodeBase64(baos.toByteArray())); SharedPreferences.Editor editor = mSharedPreferences.edit(); editor.putString("person", personBase64); editor.commit(); } catch (IOException e) { e.printStackTrace(); } } }
2. Person.java
package powerise.demo01; import java.io.Serializable; import java.util.Date; public class Person implements Serializable { private static final long serialVersionUID = 4318838659250781721L; private int id; private String name; private Date birthday; public Person(int id, String name, Date birthday) { this.id = id; this.name = name; this.birthday = birthday; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", birthday=" + birthday + "]"; } }
3. main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="编号:" /> <EditText android:id="@+id/et_id" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="姓名:" /> <EditText android:id="@+id/et_name" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/btn_save" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="保存" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="结果:" /> <TextView android:id="@+id/tv_result" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>
小记:
保存 Object 对象, 最终是以 String 形式保存在 SharedPreferences 中.
首先将 Object 对象放入到 Object输出流中(ObjectOutputStream), 再用 Base64 将流转换 byte[]
读取 Object 对象, 则相反
注意: 要在 http://commons.apache.org/codec/download_codec.cgi 中下载 Base64 jar 包并添加到 BuildPath 中.
以下是常用到的Intent的URI及其示例,包含了大部分应用中用到的共用Intent。
一、打开一个网页,类别是Intent.ACTION_VIEW
Uri uri = Uri.parse(“http://blog.3gstdy.com/”);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
二、打开地图并定位到一个点
Uri uri = Uri.parse(“geo:52.76,-79.0342″);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
三、打开拨号界面 ,类型是Intent.ACTION_DIAL
Uri uri = Uri.parse(“tel:10086″);
Intent intent = new Intent(Intent.ACTION_DIAL, uri);
四、直接拨打电话,与三不同的是,这个直接拨打电话,而不是打开拨号界面
Uri uri = Uri.parse(“tel:10086″);
Intent intent = new Intent(Intent.ACTION_CALL, uri);
五、卸载一个应用,Intent的类别是Intent.ACTION_DELETE
Uri uri = Uri.fromParts(“package”, “xxx”, null);
Intent intent = new Intent(Intent.ACTION_DELETE, uri);
六、安装应用程序,Intent的类别是Intent.ACTION_PACKAGE_ADDED
Uri uri = Uri.fromParts(“package”, “xxx”, null);
Intent intent = new Intent(Intent.ACTION_PACKAGE_ADDED, uri);
七、播放音频文件
Uri uri = Uri.parse(“file:///sdcard/download/everything.mp3″);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setType(“audio/mp3″);
八、打开发邮件界面
Uri uri= Uri.parse(“mailto:admin@3gstdy.com”);
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
九、发邮件,与八不同这里是将邮件发送出去,
Intent intent = new Intent(Intent.ACTION_SEND);
String[] tos = { “admin@3gstdy.com” };
String[] ccs = { “webmaster@3gstdy.com” };
intent.putExtra(Intent.EXTRA_EMAIL, tos);
intent.putExtra(Intent.EXTRA_CC, ccs);
intent.putExtra(Intent.EXTRA_TEXT, “I come from http://blog.3gstdy.com”);
intent.putExtra(Intent.EXTRA_SUBJECT, “http://blog.3gstdy.com”);intent.setType(“message/rfc882″);
Intent.createChooser(intent, “Choose Email Client”);
//发送带附件的邮件
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_SUBJECT, “The email subject text”);
intent.putExtra(Intent.EXTRA_STREAM, “file:///sdcard/mysong.mp3″);
intent.setType(“audio/mp3″);
startActivity(Intent.createChooser(intent, “Choose Email Client”));
十、发短信
Uri uri= Uri.parse(“tel:10086″);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.putExtra(“sms_body”, “I come from http://blog.3gstdy.com”);
intent.setType(“vnd.Android-dir/mms-sms”);
十一、直接发邮件
Uri uri= Uri.parse(“smsto://100861″);
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra(“sms_body”, “3g android http://blog.3gstdy.com”);
十二、发彩信
Uri uri= Uri.parse(“content://media/external/images/media/23″);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(“sms_body”, “3g android http://blog.3gstdy.com”);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType(“image/png”);
十三、# Market 相关
1 //寻找某个应用
Uri uri = Uri.parse(“market://search?q=pname:pkg_name”);
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//where pkg_name is the full package path for an application
2 //显示某个应用的相关信息
Uri uri = Uri.parse(“market://details?id=app_id”);
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//where app_id is the application ID, find the ID
//by clicking on your application on Market home
//page, and notice the ID from the address bar
十四、路径规划
Uri uri = Uri.parse(“http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en”);
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//where startLat, startLng, endLat, endLng are a long with 6 decimals like: 50.123456