安卓项目搭建总结

来源:互联网 发布:天龙八部抢号软件 编辑:程序博客网 时间:2024/06/05 08:14

最近从0搭建一个项目,从项目目录到各方面细节和一些框架上考虑。现在给各位大大分享下吧!

1.项目结构(本项目采用MVP,根据自己需求更改)
Trunk

  • app——Application Activity Fragment Presenter等的顶级父类,比如BaseActivity、BaseFragment、MyApplication、BasePresenter等.
  • config——API,常量表等,比如NetworkAPI(网络连接地址,常用常量类).
  • model——数据层
    - entities——数据模型
    - implement——Model实现类
  • presenter——MVP的P
  • service——服务
  • ui——MVP的V,下一级根据模块名称而定。
    • UserManage
    • map
  • utils——工具类集合
  • widget——各个可复用View集合

    2.项目工具类

百度上太多了我就不贴出来了,比如常用的Toast、日志工具类L.java、屏幕相关辅助类 ScreenUtils、还有一些时间辅助类等自己根据需求百度下吧!

http://blog.csdn.net/lmj623565791/article/details/38965311

3.项目字体
在实际开发中使用TextView组件,但是因为TextView使用android 系统本身内置了一些字体所以字体大小根据系统字体大小而定,所以导致布局错乱所以我们需要考虑使用自定义字体。
android系统支持TypeFace,即ttf的字体文件。我们可以在程序中放入ttf字体文件,在程序中使用Typeface设置字体。ttf的字体文件我们可以通过自己制作或者去网络上下载,找到ttf的字体文件后放入工程assets/fonts/目录下面 ,使用一下代码加载字体。

TextView tv = (TextView)findViewById(R.id.custom_font);
Typeface face = Typeface.createFromAsset ( getAssets() , “fonts/timesi.ttf” );
 tv.setTypeface (face);

但是在实际项目中每次加载一次ttf字体文件是很浪费内存的一种行为我们就可以考虑使用自定义TextView组件来替换所有的TextView
自定义一个CustomTextView类 继承自TextView 在构造函数内修改字体
在自定义TextView中注意把加载ttf字体文件弄成静态的一次加载全局使用。
这样的办法需要大量替换页可以采用一些Github上第三方框架也是可以的。
本菜鸟使用的自定义字体。

https://github.com/chrisjenx/Calligraphy

3.适配屏幕适配的dimens.xml
在项目中经常需要使用layout文件引用dimen单位变量来适配不同屏幕,刚开始还傻傻的一个个定义。在某天一个偶然的机遇下遇到一篇好文章,使用了下效果不错推荐给各位大大。

自动生成Android屏幕适配的dimens.xml
http://blog.csdn.net/offbye/article/details/48658097

生成的工具类。

import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;/** * 根据values/dimens.xml, 自动计算比例并生成不同分辨率的dimens.xml * 注意用dp和sp,不要用dip,否则生成可能会出错;xml值不要有空格 * Created by zhangxitao on 15/9/22. */public class DimenTool {    public static void gen() {        File file = new File("./app/src/main/res/values/dimens.xml");        BufferedReader reader = null;        StringBuilder sw480 = new StringBuilder();        StringBuilder sw600 = new StringBuilder();        StringBuilder sw720 = new StringBuilder();        StringBuilder sw800 = new StringBuilder();        StringBuilder w820 = new StringBuilder();        try {            System.out.println("生成不同分辨率:");            reader = new BufferedReader(new FileReader(file));            String tempString;            int line = 1;            // 一次读入一行,直到读入null为文件结束            while ((tempString = reader.readLine()) != null) {                if (tempString.contains("</dimen>")) {                    //tempString = tempString.replaceAll(" ", "");                    String start = tempString.substring(0, tempString.indexOf(">") + 1);                    String end = tempString.substring(tempString.lastIndexOf("<") - 2);                    int num = Integer.valueOf(tempString.substring(tempString.indexOf(">") + 1, tempString.indexOf("</dimen>") - 2));                    sw480.append(start).append((int) Math.round(num * 0.6)).append(end).append("\n");                    sw600.append(start).append((int) Math.round(num * 0.75)).append(end).append("\n");                    sw720.append(start).append((int) Math.round(num * 0.9)).append(end).append("\n");                    sw800.append(tempString).append("\n");                    w820.append(tempString).append("\n");                } else {                    sw480.append(tempString).append("\n");                    sw600.append(tempString).append("\n");                    sw720.append(tempString).append("\n");                    sw800.append(tempString).append("\n");                    w820.append(tempString).append("\n");                }                line++;            }            reader.close();            System.out.println("<!--  sw480 -->");            System.out.println(sw480);            System.out.println("<!--  sw600 -->");            System.out.println(sw600);            System.out.println("<!--  sw720 -->");            System.out.println(sw720);            System.out.println("<!--  sw800 -->");            System.out.println(sw800);            String sw480file = "./app/src/main/res/values-sw480dp-land/dimens.xml";            String sw600file = "./app/src/main/res/values-sw600dp-land/dimens.xml";            String sw720file = "./app/src/main/res/values-sw720dp-land/dimens.xml";            String sw800file = "./app/src/main/res/values-sw800dp-land/dimens.xml";            String w820file = "./app/src/main/res/values-w820dp/dimens.xml";            writeFile(sw480file, sw480.toString());            writeFile(sw600file, sw600.toString());            writeFile(sw720file, sw720.toString());            writeFile(sw800file, sw800.toString());            writeFile(w820file, w820.toString());        } catch (IOException e) {            e.printStackTrace();        } finally {            if (reader != null) {                try {                    reader.close();                } catch (IOException e1) {                    e1.printStackTrace();                }            }        }    }    public static void writeFile(String file, String text) {        PrintWriter out = null;        try {            out = new PrintWriter(new BufferedWriter(new FileWriter(file)));            out.println(text);        } catch (IOException e) {            e.printStackTrace();        }        out.close();    }    public static void main(String[] args) {        gen();    }}

4.主题设置
1.先在color.xml中写好需要的颜色:

<resources>    <color name="Orange">#ff5722</color>    <color name="DeepPurple">#673AB7</color>    <color name="DeepPurple900">#311B92</color>    <color name="White">#fff</color>    <color name="Gray">#888888</color>    <color name="Gray100">#dddddd</color>    <color name="Gray600">#999999</color></resources> 

2.设置好基础style.xml主题:

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">    <!-- Customize your theme here. -->    <item name="colorPrimary">@color/DeepPurple</item>    <item name="colorPrimaryDark">@color/DeepPurple900</item>    <item name="colorAccent">@color/Orange</item></style><style name="AppTheme" parent="AppTheme.Base"></style>

然后就开始考虑下沉浸式和主题更换的东西吧!
沉浸式实现:

转载自:
http://blog.csdn.net/lmj623565791/article/details/48649563;
本文出自:【张鸿洋的博客】
换肤框架:
https://github.com/fengjundev/Android-Skin-Loader
https://github.com/qqliu10u/QSkinLoader
网上太多了!就不浪费时间了。。

5.定义开发规范
为了避免合作开发写的代码风格迥异。或做出了多套开发模式。下面是个例子。毕竟是为了高效开发而制定的。适合自己项目的才是最好。
所有Activity继承BaseActivity
所有Fragment继承BaseFragment
所有Presenter继承BasePresenter
这样利于生命周期管理。也可以方便的全局修改。
命名,例
AccountFragment
UserDetailActivity

layout命名(模块名+组件类型),例
main_activity
account_fragment
person_item
include_toolbar
view_progress

id命名,例
send_btn
name_tv
user_list
password_et

变量命名:以m开头。例mAdapter使用时按一个m全都出来了
方法命名:与其写好名字不如写好注释。= =。

然后选取框架进行开发。。
奉上

Android 流行框架查速表
http://www.ctolib.com/cheatsheets-Android-ch.html
谢谢各位大大,项目后续开发我会持续更新项目中的问题与大家分享。请各位大大指点。。。

0 0
原创粉丝点击