Blogroll
Categories
Archives
- February 2012
- January 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- May 2011
- April 2011
- March 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
Category Archives: android
[android]设置Spinner弹出框/下拉框的样式
Spinner drop-down list 默认的样式比较丑陋的,默认效果图见上一篇文章Android Spinner例子。 所以我们要自定义dropdown list的样式,下拉框的样式不能直接在Spinner标签里定义的,但也非常简单。 如下所以: 1. Define layout XML 在res/layout/下新建一个XML文件-drop_down_item.xml ?View Code XML<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:textColor="#000000" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="12dip"> </TextView> 此layout XML是作用于下拉列表中一项的样式。 2. setDropDownViewResource ?View Code JAVAArrayAdapter<String> accountTypesAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_type); accountTypesAdapter.setDropDownViewResource(R.layout.account_type_drop_down_item); spinner.setAdapter(accountTypesAdapter); … Continue reading
Android的沙箱模式
一旦安装到设备上,每个Android应用都存活在各自的安全沙箱中: Android OS是多用户的Linux系统,每个应用都被分配给不同用户。 默认情况下,系统给每个应用分配唯一的Linux user ID(User ID只给系统使用,应用本身是不知道的)。系统为每一个应用下所有的文件都设置正确的权限,只有属于该应用的user ID才能访问它们。 每个进程都拥有专属的虚拟机,所以应用代码的运行跟另外的应用都是独立开来的。 默认下,每个应用都运行在自己专属的Linux进程。当一个应用中的任何一个组件要执行的时,Android会先为它启动进程;当应用不用的时或者必须要为其他应用腾出内存时,Android会关闭该进程。
Android Spinner例子
在Android中, Spinner组件就是下拉列表. 弹出一个选项列表,允许你从列表中选择一项。 布局的XML文件如下 ?View Code XML<Spinner android:id="@+id/Spinner01" android:layout_width="wrap_content" android:layout_height="wrap_content" /> Activity ?View Code JAVA1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class SpinnerExample extends Activity { private String … Continue reading
Posted in android, 编程开发
5 Comments
android自动关闭(隐藏)提示框(对话框)
正在做一个小项目,实现修改密码的功能,当输入的旧密码不正确时,弹出一个提示然后自动消失。 代码如下: ?View Code JAVAif (!settingInfo.getString(KEY_PWD, "132465").equals(oldPwd)) { Toast.makeText(getApplicationContext(), "Old password is wrong", Toast.LENGTH_SHORT).show(); return; } A toast notification is a message that pops up on the surface of the window. It only fills the amount of space required for … Continue reading
Android的LayoutInflater
LayoutInflater是用来把XML布局文件转换成Android的View对象。它从来不是直接实例化出来的,取而代之的是使用getLayoutInflater() or getSystemService(String)获取跟当前context绑定一起的LayoutInflater实例,这个实例跟当前运行的设备也正确的配置一起。 得到LayoutInflater实例的三个方法: LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); LayoutInflater inflater = ((Activity) this).getLayoutInflater(); LayoutInflater inflater = LayoutInflater.from(getApplicationContext()) 生成View对象 View contentView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.add_account, null);
Android实现底部菜单栏和顶部菜单栏
Android App的需求:同时显示顶部菜单栏和底部菜单栏,要一直显示在屏幕上,中间是一个可滚动的动态列表。 下面是实现的简单代码 main.xml包含top menu, bottom menu以及动态列表的占位符。 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” > <!– top menu –> <LinearLayoutandroid:id=“@+id/top_menu” android:layout_width=“fill_parent” android:layout_height=“wrap_content” > <TextView android:id=“@+id/top” android:layout_width=“fill_parent” android:layout_height=“40px” android:background=“@drawable/red” android:text=“top static view” /> </LinearLayout> <!– placeholder for list –> <ListView android:id=“@android:id/list” android:layout_width=“fill_parent” android:layout_height=“wrap_content” android:layout_weight=“1″ /> <!– bottom menu –> <LinearLayout android:id=“@+id/bottom_meunun” android:layout_width=“fill_parent” android:layout_height=“wrap_content” > <TextView android:id=“@+id/bottom” android:layout_width=“fill_parent” … Continue reading
Android开发入门之如何使用ListView
ListView是Android经常用到的控件,它用来展示可以滚动的列表。ListView里的列表项是通过adapter来添加的,adapter必须是继承“BaseAdapter”,它的职责是提供数据模型,以及把数据转换成列表项。 Android自带ArrayAdapter、SimpleAdapter和CursorAdapter ArrayAdapter处理数组或者列表里的数据,通常列表项只有一个文本。 CursorAdapter主要针对数据使用。 SimpleAdapter处理列表里的数据,不同于ArrayAdapter,SimpleAdapter每一个列表项由一个Map对象提供数据。 SimpleAdapter实现ListView的步骤: 1. 定义列表项的layout 2. 准备ListView要显示的数据 ; 3. 把数据添加到数组里; 4. 构建适配器 , 数据适配器 , 动态数组有多少元素就生成多少个列表项; 5. 把适配器 添加到ListView,并显示出来。 1.定义列表项的layout,某一项的layout,并不是整个List的layout。 ?View Code XML<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <TextView android:text="TextView" android:id="@+id/accountType" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10pt" android:background="#FFFFFF" android:width="30pt"></TextView> <TextView android:text="TextView" … Continue reading
跟踪android运行状态日志
如果你不喜欢用android debugger(因为它比较慢,效率不高或者其他原因),那你可以使用StackTraceElement跟踪应用活动记录. 你不需要在每个方法都加上System.out。 只要加上LogUtils.trace(),log就会输出方法名称和代码行数。 Here’s simple sample LogUtils class: ?View Code JAVApackage com.test.log; // !!!!! // NOTE: enabled log significantly reduces performance // because it uses high cost getStackTrace() method, which produces huge amounts of objects // !!!!! public … Continue reading
Android性能优化技巧
•Do not allocate memory as much as you can 尽量少分配内存 •GC is slow (~x00 ms on Android device) •Decrease number of objects – GC will work faster (i.e. StringBuffer(or StringBuilder) vs String) 减少对象的创建 •Use primitive types for arrays (int vs … Continue reading
用Intents在Activities之间传输数据
Activities之间传输数据有多种方式,其中一种方式就是用Intent来传输数据。 本文的例子就是展示了一个Java对象如何通过Intent在Activities之间传输。 基本思想: • 序列化所有即将要传输的数据。 • 把要传输的对象添加到Intent里。 • 启动intent 传输数据里所涉及的到对象都要实现Serializable接口(Serializable just a mark interface) implements java.io.Serializable 要传输的对象 需要指出的是所有的字段都是public的,虽然这不是很OO,但是为了减少方法调用的开支,在Android里还是推荐这种方式。 package com.hwk.intent; public class Player implements java.io.Serializable { public int id; public char zhType; public float gpx; public float gpy; public int … Continue reading