您的位置:首页 > 移动开发 > Android开发

android 编程代码规范

2012-10-28 19:40 267 查看
学习android开发已经有很长时间了,但是有时代码却很少用规范的模式进行书写,下面就简要的总结了自己学习的代码规范。

一、关于一些常量值资源的书写规范

1.颜色值

颜色值有RGB和透明信息Alpha组成,以#开头, 形式有 #RGB #ARGB #RRGGBB #AARRGGBB

一般存储于res/values/colors.xml 中 必须包含的头文件(<?xml version="1.0" encoding="utf-8"?>)和一个根节点(<resources></resources>)

代码中的使用是R.color.black xml中的使用是“@color/black(” black是-----> (颜色名)

<?xml version="1.0" encoding="utf-8"?>
<!-- 必须添加的头文件 -->
<resources>

<!-- 颜色值资源的定义 -->
<color name="black">#000000</color>
<color name="grey">#C0C0C0</color>
<color name="white">#FFFFFF</color>
<color name="orange">#FF6100</color>

</resources>


2.字符串和格式化文本
一般存储于res/values/strings.xml 中 必须包含的头文件(<?xml version="1.0" encoding="utf-8"?>)和一个根节点(<resources></resources>)

代码中使用是R.string.me(字符串名称)XML中使用“@string/me(字符串名称)”

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="hello">Hello World, AndroidFromatActivity!</string>
<string name="app_name">AndroidFromat</string>
<string name="me">王杰</string>
<string name="QQ号码">1150580768</string>
<string name="联系电话">15290336267</string>

</resources>


3.大小值(px、in、mm、pt、dp、sp)
px(Pixel 像素)对应是实际屏幕是像素

in(Inch 英寸)基于实际屏幕的物理大小

mm(Millimeter 毫米) 基于实际屏幕的物理大小

dp或者dip(Density-independent Pixel 密度无关像素) 基于屏幕的的物理点阵密度

sp(Scale-independent Pixel 比例无关像素)与dp类似,不同之处,该单位可以根据用户的字体大小选择进行比例调节

一般存储于res/values/dimens.xml 中 必须包含的头文件(<?xml version="1.0" encoding="utf-8"?>)和一个根节点(<resources></resources>)

代码中使用是R.dimen.test_px(字符串名称)
XML中使用“@dimen/test_px(字符串名称)”

<?xml version="1.0" encoding="utf-8"?>
<!-- 必须添加的头文件 -->
<resources>

<!-- 大小值资源的定义 -->

<dimen name="test_px">10px</dimen>
<dimen name="test_in">10in</dimen>
<dimen name="test_mm">10mm</dimen>
<dimen name="test_pt">10pt</dimen>
<dimen name="test_dp">10dp</dimen>
<dimen name="test_dip">10dip</dimen>
<dimen name="test_sp">10sp</dimen>

</resources>


4.数组资源

支持字符串(string)和整形(integer)的数组

一般存储于res/values/arrays.xml 中 必须包含的头文件(<?xml version="1.0" encoding="utf-8"?>)和一个根节点(<resources></resources>)

代码中使用是R.array.select_week(字符串名称)XML中使用“@array/select_week(字符串名称)”

<?xml version="1.0" encoding="utf-8"?>
<!-- 必须添加的头文件 -->
<resources>

<!-- 数组资源的定义 -->
<string-array name="select_week">
<item> 星期一</item>
<item> 星期二</item>
<item> 星期三</item>
<item> 星期四</item>
<item> 星期五 </item>
<item> 星期六</item>
<item> 星期日</item>
</string-array>

<integer-array name="select_classtime">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
</integer-array>

</resources>
5.界面样式(皮肤)和主题

一般存储于res/values/styles.xml 中 必须包含的头文件(<?xml version="1.0" encoding="utf-8"?>)和一个根节点(<resources></resources>)

代码中使用是R.style.style_parent(字符串名称)XML中使用“@style/style_parent(字符串名称)”

<?xml version="1.0" encoding="utf-8"?>
<!-- 必须添加的头文件 -->
<resources>

<!-- 样式资源的定义 黑体 20dp大小 子样式的字体是橙色 -->

<style name="style_parent">
<item name="android:textStyle">bold</item>
<item name="android:textSize">20dp</item>
</style>

<style name="style_child" parent="@style/style_parent">
<item name="android:textColor">#FF6100</item>
</style>

<!-- 主题资源的定义 -->

<style name="theme_dialog" parent="android:Theme.Dialog">
<item name="android:background">#F0FFFF</item>
<item name="android:textColor">#FF6100</item>
<item name="android:textSize">20dp</item>
</style>

<style name="theme_panel" parent="android:Theme.Panel">
<item name="android:background">#00FFFF</item>
<item name="android:textColor">#FF6100</item>
<item name="android:textSize">20dp</item>
</style>

</resources>

6.动画资源

一般存储于res/anim/animation.xml 中 可以不用包含的头文件(<?xml version="1.0" encoding="utf-8"?>)和一个根节点(<resources></resources>)

代码中使用是R.anim.animation(文件名)XML中使用“@anim/animation(文件名)”

<?xml version="1.0" encoding="utf-8"?>
<!-- 动画资源可以不添加的头文件 -->
<resources>

<!-- 动画资源的定义 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<!-- 移动动画 -->
<translate
android:duration="3000"
android:fromXDelta="50%p"
android:toXDelta="0" />
<!-- 透明度 -->
<alpha
android:duration="3000"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
<!-- 旋转 -->
<rotate
android:duration="3000"
android:fromDegrees="-360"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="0" />
<!-- 缩放 -->
<scale
android:duration="3000"
android:fromXScale="50"
android:fromYScale="50"
android:toXScale="100"
android:toYScale="1000" />
</set>

</resources>


7.菜单资源

一般存储于res/menu/menu_option.xml 中 同文件一般是自动生成的<menu xmlns:android="http://schemas.android.com/apk/res/android"></menu>

代码中使用是R.menu.menu_options(文件名)XML中使用“@anim/menu_options(文件名)”

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- menu 资源选项 -->
<group>
<item
android:id="@+id/menuhelp"
android:icon="@drawable/help"
android:title="help">
</item>
<item
android:id="@+id/menuabout"
android:icon="@drawable/about"
android:title="about">
</item>
</group>
<group>
<item
android:id="@+id/menuadd"
android:icon="@drawable/add"
android:title="add">
</item>
<item
android:id="@+id/menuexit"
android:icon="@drawable/exit"
android:title="exit">
</item>
</group>

</menu>


8.文件资源

文件资源分为XML文件、原生文件资源

xml文件资源一般位于res/xml/test.xml 这里的xml文件可以不遵循android的规范术语

原生文件资源一般位于res/raw/demo.txt等等资源类型的文件

9.备选资源

比如,横屏和竖屏的图片处理: 横向图片存放:res/drawable-land文件夹中 纵向图片:res/drawable-port文件夹中

10.系统资源类型的定义说明

android系统资源类型

资源值类型定义说明
动画定义于R.anim类中
数组定义于R.array类中
属性值定义于R.attr类中
颜色值定义于R.color 类中
大小值定义于R.dimen 类中
绘制用定义于R. drawable 类中
ID定义于R. id类中
整数定义于R.integer类中
布局定义于R.layout类中
字符串定义于R.string类中
样式和主题定义于R.style类中
11.android Dos下是命令行

1、adb 工具

连接模拟器

adb shell

上传文件到模拟器中

adb push <本地路径><远程文件路径>

从模拟器中下载文件

adb pull <远程文件路径>
<本地路径>

安装包文件管理

adb install <本地包文件>

移除包文件

adb uninstall <包名>

2、sqlite3 工具

打开或者创建数据库

sqlite3 <数据库路径>

查看数据库版本

sqlite3 <version>

3、keytool 工具

创建密钥库文件

keytool -genkey -keystore<密钥库路径> -alias<密钥库别名>-keyalg<密钥算法>

列举密钥库信息

keytool -list -keystore <密钥库路径>

2012年11月26

导航界面只在第一次使用的时候出现

SharedPreferences preferences;
//导航界面只在第一次启动的时候用
firststartActivity();


public void firststartActivity(){

preferences = getSharedPreferences("count",MODE_WORLD_READABLE);
int count = preferences.getInt("count", 0);
//判断程序与第几次运行,如果是第一次运行则跳转到引导页面
if (count == 0) {
Intent intent = new Intent();
intent.setClass(getApplicationContext(),Navigation.class);
startActivity(intent);
finish();
}
Editor editor = preferences.edit();
//存入数据

editor.putInt("count", ++count);
//提交修改

editor.commit();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: