Android开发常用工具类与资源
2016-05-31 13:04
330 查看
常用初始化类
public class ContextUtils extends Application { /* 获取context */ private static ContextUtils instance; public static ContextUtils getInstance() { return instance; } @Override public void onCreate() { super.onCreate(); instance = this; initValue(); } public void initValue() { //LogUtils.DEBUG = true; } }
当然,需配合AndroidMainfest.xml使用
<application android:name=".util.ContextUtils" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
常用日志工具类
public class LogUtils { public static boolean DEBUG; public static void v(String msg) { if (DEBUG) Log.v(getMethodPath(4, 4), msg); } public static void i(String msg) { if (DEBUG) Log.i(getMethodPath(4, 4), msg); } public static void d(String msg) { if (DEBUG) Log.d(getMethodPath(4, 4), msg); } public static void w(String msg) { if (DEBUG) Log.w(getMethodPath(4, 4), msg); } public static void e(String msg) { if (DEBUG) Log.e(getMethodPath(4, 4), msg); } /** * 得到调用此方法的类名与方法名,默认下标为3 * @return string */ public static String getMethodPath() { return Thread.currentThread().getStackTrace()[3].getClassName() + "." + Thread.currentThread().getStackTrace()[3].getMethodName() + "-->"; } /** * 得到调用此方法的类名与方法名 * @param classPrior 类级 * @param methodPrior 方法级 * @return string */ public static String getMethodPath(int classPrior, int methodPrior) { int length = Thread.currentThread().getStackTrace().length; if (classPrior > length || methodPrior > length) { return null; } else return Thread.currentThread().getStackTrace()[classPrior].getClassName() + "." + Thread.currentThread().getStackTrace()[methodPrior].getMethodName() + "-->"; } /** * 测试方法,将线程中的序列全部输出 */ public static void logThreadSequence() { int length = Thread.currentThread().getStackTrace().length; for (int i = 0; i < length; i++) { Log.i(Thread.currentThread().getStackTrace()[i].getClassName(), Thread.currentThread().getStackTrace()[i].getMethodName()); } } }
利用这个类,LOG工具就再也不用定义TAG了,调用LOG的类与方法名都会打印出来。
常用颜色资源color.xml
<?xml version="1.0" encoding="utf-8" ?> <resources> <color name="beige">#F5F5DC</color> <!--米色 --> <color name="black">#000000</color> <!--黑色 --> <color name="darkgrey">#99A9A9A9</color> <!--暗灰色 --> <color name="darkgrey2">#992B2B2B</color> <!--透明 --> <color name="transparent">#00000000</color> <!--纯黄色 --> <color name="lightyellow">#FFFFE0</color> <!--亮黄色--> <color name="lavenderblush">#FFF0F5</color> <!--淡紫红 --> <color name="mistyrose">#FFE4E1</color> <!--浅玫瑰色 --> <color name="gold">#FFD700</color> <!--金色 --> <color name="fuchsia">#FF00FF</color> <!--紫红色 --> <color name="azure">#F0FFFF</color> <!--天蓝色 --> <color name="palevioletred">#DB7093</color> <!--苍紫罗兰色 --> <color name="darkviolet">#9400D3</color> <!--暗紫罗兰色 --> <color name="lightskyblue">#87CEFA</color> <!--亮天蓝色 --> <color name="skyblue">#87CEEB</color> <!--天蓝色 --> <color name="darkred">#8B0000</color> <!--暗红色 --> <color name="blueviolet">#8A2BE2</color> <!--紫罗兰蓝色 --> <color name="aqua">#00FFFF</color> <!--浅绿色 --> <color name="cyan">#00FFFF</color> <!--青色 --> <color name="springgreen">#00FF7F</color> <!--春绿色--> </resources>
常用尺寸资源dimens.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="dimen_1">1dp</dimen> <dimen name="dimen_2">2dp</dimen> <dimen name="dimen_4">4dp</dimen> <dimen name="dimen_5">5dp</dimen> <dimen name="dimen_10">10dp</dimen> <dimen name="dimen_15">15dp</dimen> <dimen name="dimen_18">18dp</dimen> <dimen name="dimen_20">20dp</dimen> <dimen name="dimen_22">22dp</dimen> <dimen name="dimen_25">25dp</dimen> <dimen name="dimen_28">28dp</dimen> <dimen name="dimen_30">30dp</dimen> <dimen name="dimen_32">32dp</dimen> <dimen name="dimen_36">36dp</dimen> <dimen name="dimen_40">40dp</dimen> <dimen name="dimen_50">50dp</dimen> <dimen name="dimen_60">60dp</dimen> <dimen name="dimen_70">70dp</dimen> <dimen name="dimen_80">80dp</dimen> <dimen name="dimen_100">100dp</dimen> <dimen name="dimen_120">120dp</dimen> <dimen name="dimen_140">140dp</dimen> <dimen name="dimen_150">150dp</dimen> <dimen name="dimen_160">160dp</dimen> <dimen name="dimen_180">180dp</dimen> <dimen name="dimen_200">200dp</dimen> <dimen name="text_10">10sp</dimen> <dimen name="text_12">12sp</dimen> <dimen name="text_14">14sp</dimen> <dimen name="text_16">16sp</dimen> <dimen name="text_18">18sp</dimen> <dimen name="text_20">20sp</dimen> <dimen name="text_22">22sp</dimen> <dimen name="text_24">24sp</dimen> <dimen name="text_28">28sp</dimen> <dimen name="text_32">32sp</dimen> <dimen name="text_36">36sp</dimen> <dimen name="text_40">40sp</dimen> </resources>
相关文章推荐
- Awstats处理多apache日志
- 自学成才的秘密:115个 web Develop 资源
- C#、ASP.NET通用扩展工具类之TypeParse
- 典型入侵日志分析
- MS SQL Server数据库清理错误日志的方法
- 对MySQL日志操作的一些基本命令总结
- 简介操作MySQL日志的一些方法
- MySQL的日志基础知识及基本操作学习教程
- MSSQL 2005/2008 日志压缩清理方法小结
- SQL Server误区30日谈 第19天 Truncate表的操作不会被记录到日志
- C#、ASP.NET通用扩展工具类之LogicSugar
- C#3.0使用EventLog类写Windows事件日志的方法
- C#、ASP.NET通用工具类IsWhat?(可以判断数字、身份证、数据类型等等)
- ORA-00392ORA-00312日志正在清除故障
- MySQL中查询日志与慢查询日志的基本学习教程
- mysql binlog二进制日志详解
- C#中使用资源的方法分析
- Shell脚本实现Linux系统和进程资源监控
- C#实现线程安全的简易日志记录方法
- C#实现将程序运行信息写入日志的方法