Android Snippet

1.获取屏幕的分辨率

在 Activity 里使用如下代码,宽度和高度的单位是像素

Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();

2.绘制文本

使用 FontMetrics 类

参考

http://www.javaeye.com/topic/474526

3.禁止自动横竖屏切换

在AndroidManifest.xml的Activity节点加入如下属性

android:screenOrientation="portrait"

portrait是纵向,landscape是横向

4.Resources and Assets

无论是使用Res\raw还是使用Asset存储资源文件,文件大小UNCOMPRESS限制为1MB

参考

http://wayfarer.javaeye.com/blog/547174

5.SDK 1.6 新增加SD卡写入权限

在AndroidManifest.xml加入以下代码

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

SDK1.6之前的项目自适应,无需此权限也可以写入SD卡。

6.在eclipse中查看Android Framework源代码

代码下载

参考

http://www.javaeye.com/topic/534010

7.给View注册ContextMenu

void setOnCreateContextMenuListener(View.OnCreateContextMenuListener l)

Register a callback to be invoked when the context menu for this view is being built.

8.给Preference设置Intent

void setIntent(Intent intent)

Sets an Intent to be used for startActivity(Intent) when this Preference is clicked.

9.包含CheckBox的ListView

ListView item中加入checkbox后onListItemClick 事件无法触发。
原因:checkbox的优先级高于ListItem于是屏蔽了ListItem的单击事件。
解决方案:设置checkbox的android:focusable="false"

原文地址:https://www.cnblogs.com/shaobin0604/p/1764884.html