Andoid笔记

  • 名词解释:
    • View: usually UI widgets such as a button or text field
    • ViewGroup: invisible view container that define how the child views are laid out, such as in a grid or a vertical list
    • Activity: a single screen in your app
    • Intent: runtime binding between separate components (such as two activities). An Intent can be explicit in order to start a specific component (a specific Activity instance) or implicit in order to start any component that can handle the intended action (such as “capture a photo”).
    • Bundle: a blob of key-value pairs
    • Fragment: a nested activity that can define its own layout and manage its own lifecycle
  • Layout:
    • layout_file.xml中的属性:

      • xmlns:android=xxx: 声明xml名称空间android
      • android:id="@+id/edit_message": 声明id属性,使代码中可引用它
        @: resource object, an unique integer for an resource (bitmap, layout file, string) defined in your projects’ gen/R.java file
        +: create
        id: resource type
        edit_message: resource name, other resource type can use the same name
      • 引用resource object时还可加上namespace,如android:hint="@**android:**string/edit_message",这时引用的是android.R
    • 每个layout_file.xml会被编译成一个View,可在Activity的onCreate()中load它:setContentView(R.layout.layout_file)

    • 可在layout_file.xml中定义view并赋予id:

        <Button android:id="@+id/my_button" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="@string/my_button_text" />

      然后在代码中create它:Button myButton = (Button) findViewById(R.id.my_button);

    • View的属性layout_something定义其在ViewGroup中的layout。layout_widthlayout_height是所有View都须定义的,可定义值:

      • wrap_content: tells your view to size itself to the dimensions required by its content
      • fill_parent/match_parent(rename in API Level 8): tells your view to become as big as its parent view group will allow
      • n dp: density-independent pixel units
  • Activity lifecycle:
    • Created/Started是临时状态,Resumed/Paused/Stopped是可停留状态。Resumed指正常running,Paused指失去焦点被其他组件部分遮挡,Stopped指被其他组件完全遮挡。
    • 在onDestroy()前都会调onPause()和onStop(),除非在onCreate()直接调finish()
Activity lifecycle
  • AndroidManifest.xml:

    • 所有Activity得在AndroidManifest.xml中用<activity>元素声明

    • 启动app时进入的main activity声明时需要<intent-filter>:

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
  • android版本分布

  • android icon size: 36×36 (ldpi), 48×48 (mdpi), 72×72 (hdpi), 96×96 (xhdpi)

  • 读取AndroidManifest.xml中application的meta-data:

      try {
          ApplicationInfo ai = this.getPackageManager().getApplicationInfo(this.getPackageName(), PackageManager.GET_META_DATA);
          String channelName = ai.metaData.getString("UMENG_CHANNEL");
          if (channelName == null) return "";
          return channelName;
      } catch (NameNotFoundException ex) {
          Log.e("META", "Can not find the application info of" + mActivity.getPackageName());
          return "";
      }

参考: