天天看点

Android基础总结(一)

1. AndroidManifest标签重点汇总

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.base.module.recorder"
    android:versionCode="1"
    android:versionName="1.0"
    android:sharedUserId="android.uid.system"
  android:installLocation="auto|internalOnly|preferExternal"
    >
           

manifest标签package为项目包属性, versionCode为版本整数,每次迭代则数字增加,versionName为显示给用户的公共版本号。

1.1 常用标签介绍

  • installLocation

    属性指定是否允许(或者首选)将应用程序安装到外部存储器(SD卡)而不是内部存储器。preferExternal将应用程序首选安装到外部存储器,auto则要求系统决定。

    注意:下列程序不适合安装到外置存储:对于具有Widget、Live Wallpaper和Live Folder的应用程序;提供不中断服务的应用程序;输入法引擎;设备管理器 DeviceAdminReceiver及其管理能力将被禁用。

  • uses-sdk

    该节点用于定义要想正确运行应用程序,设备上必须具有的最低和最高SDK版本,以及为应用程序设计的目标SDK,分别通过minSDKVersion、maxSDKVersion和targetSDKVersion属性设置。

    通常在开发中将targetSDKVersion指定为最高SDK版本。

    通常没有必要指定最高SDK版本。

<uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="27" />
           
  • uses-configuration

    可以指定应用程序支持的每个输入机制的组合,一般不需要包含,但是对于特殊输入控制的游戏很有用。

  • uses-feature

    指定应用程序需要的每个硬件功能。这样可以避免将应用程序安装到不包含必要的硬件功能(如NFC硬件)的设备。

required属性可以覆盖暗含对硬件的需求。

指定OpenGL的最低版本,高16位代表主版本号,低16位代表次版本号。

  • supports-screens
<supports-screens android:largeScreens="true"
        android:normalScreens="true"
        android:largestWidthLimitDp="720"
        android:compatibleWidthLimitDp="600"
        android:requiresSmallestWidthDp="480"
        android:anyDensity="true"
        android:smallScreens="false"
        android:xlargeScreens="false"
        android:resizeable="true">
           
  • supports-gl-texture

    用于声明应用程序能够提供的以一种特定的GL纹理压缩格式压缩的纹理资源。

  • uses-permission 作为安全模型的一部分,声明了应用程序需要。
  • permission 应用程序组件可以创建权限来限制对共享应用程序组件的访问。
<permission android:name="com.test.MYTEST"
        android:protectionLevel="dangerous"
        android:description="xx"
        android:label="xx"/>
           

访问权限的级别包括(normal | dangerous | signature | signatureOrSystem)

  • instrumentation
<instrumentation
        android:label="xx"
        android:name="xx"
        android:targetPackage="xx"/>
           
  • application

    一个Manifest只能包含一个application节点。使用各种属性来指定应用程序的各种数据(标题、图标和主题)。开发时,应该包含一个设置为true的debuggable属性启用调试,发布时禁止该属性。

<application
        android:name="com.base.module.recorder.RecorderApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:debuggable="true">
           
  • activity

    每个Activity都要求一个activity标签,允许使用intent-filter标签定义用于启动该Activity的Intent。

  • Service

    同Activity。

  • Provider 指定应用程序中每个Content Provider。用于管理数据库访问和共享。
  • receiver 注册一个Broadcast Receiver,允许使用intent-filter标签定义可以用来触发接收器的Intent。
  • uses-library 用于指定该应用程序需要的共享库。可以指定特定的一个包是必需的还是可选的。指定为必须时,在缺少指定库的设备上无法安装应用程序;指定为可选的时,应用程序在试图使用库之前,必需使用反射机制来检查该库是否存在。
<uses-library android:name="xx"
            android:required="false"/>
           

1.2 使用Manifest编辑器

ADT插件包含了一个Manifest Editor来管理Manifest,这样,就不用直接对底层的xml进行操作,直接通过gui可以添加删除相关节点。

2. 资源使用

常用资源类型如下

<string name="turn_off_auto_record">Turn off auto recording</string>
    <plurals name="androidPlurals">
        <item quantity="one">One android</item>
        <item quantity="other">%d android</item>
    </plurals>
    <color name="app_bg">#ff0000ff</color>
    <dimen name="def_border">5px</dimen>
    <string-array name="string_array">
        <item>1</item>
        <item>2</item>
        <item>3</item>
    </string-array>
    <array name="integer_array">
        <item>3</item>
        <item>2</item>
        <item>1</item>
    </array>
           

2.1 资源中引用HTML标签

Android支持简单的文本样式,所以可以使用HTML标签

来让文本字符串变为粗体、斜体及下划线。
<string name="test_string">Turn off auto recording<b>Stop.</b></string>
           

当为String.format方法输入参数时可以使用资源字符串。然而String.format不支持上面描述的文本样式。为了对一个格式化字符串使用样式,需要在创建资源时候转义为HTML标签,如下所示:

<string name="test_string">Turn off auto recording&lt;b>Stop&lt;/b>.%$s</string>
           

在代码内,可以使用Html.fromHtml方法把这些字符串转换回样式字符序列:

String reString = getString(R.string.xx);
    String fString = String.format(reString, "xx");
    CharSequence styledString = Html.fromHtml(fString);
           

2.2 字符串复数

通过plurals为字符串定义复数形式。

int unitCount = ;
    String unitString = getResources().getQuantityString(R.plurals.androidPlurals, unitCount, unitCount);
           

对象计数传入两次,一次用于返回正确的复数字符串,一次作为输出参数让句子变得完整。

2.3 颜色

2.4 尺寸

尺寸

px(像素)

in(物理英寸)

pt(物理点)

dp(非密度制约的像素)

sp(缩放比例无关的像素)

<dimen name="standard_boarder">5dp</dimen>
<dimen name="large_font_size">16sp</dimen>
           

2.5 样式和主题

样式可以继承,从而使得创建简单的变体形式变得十分简单。

<style name="parentStyle">
        <item name="android:textSize">dp</item>
        <item name="android:textColor">#112331</item>
    </style>

    <style name="childStyle" parent="parentStyle">
        <item name="android:textSize">dp</item>
    </style>
           

2.6 Drawable

包括位图和.9 png图像,也包含复杂复合Drawable,比如可以在xml中定义LevelListDrawable和StateListDrawable.

2.7 布局

五种布局

LinearLayout

RelativeLayout

FrameLayout

TableLayout

AbsoluteLayout

2.8 动画

属性动画 可以实现补间动画的功能

补间动画 旋转、缩放、平移、淡入淡出

帧动画 如视频帧播放

2.9 资源引用

  • 资源内引用
android:layout_height="@dimen/list_item_hight"
android:background="@drawable/info_background"
           
  • 引用系统资源
  • 在当前主题中引用样式

这种方式可以定义随当前主题改变而改变的样式,而不必对每个资源更改。

<activity
            android:name="com.base.module.recorder.RecordActivity"
            android:hardwareAccelerated="true"
            android:label="@string/app_name"
            android:configChanges="orientation|keyboard|mnc|locale">
        </activity>
           

2.10 其他

在Activity中复写onConfigChanged中监听这种变化,从而进行合适的处理。

Android应用程序并不能完全控制它们的生命周期,需要监听程序状态做相应改变。

在Manifest中使用Process标签可以让Activity运行在单独进程中,也可以使不同进程运行在同一个process中。

3. 应用程序优先级

系统回收资源时,进程被终止的顺序是由它们的优先级决定的,一个应用程序的优先级等同于它组件中最高优先级。

当两个进程优先级相同,谁在低的优先级上运行的时间长,谁就先被终止。

Android基础总结(一)

继续阅读