android 編程代碼規范
學習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.系統資源類型的定義說明
| 資源值類型 | 定義說明 |
| 動畫 | 定義于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 <密鑰庫路徑>