关闭

Android开发常用工具类与资源

标签: 资源工具类日志
107人阅读 评论(0) 收藏 举报
分类:

常用初始化类

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>
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4371次
    • 积分:141
    • 等级:
    • 排名:千里之外
    • 原创:10篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类