说明:该文章基于android 5.1的源码进行分析。
calculator的效果图:
packages/apps/Calculator/AndroidManifest.xml
我们先来看看这个文件AndroidManifest.xml
<-manifest
xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.android.calculator2”>
<-application
android:icon=”@mipmap/ic_launcher_calculator”
android:label=”@string/app_name”
android:theme=”@style/CalculatorTheme”>
<-activity
android:name=”.Calculator”
android:label=”@string/app_name”
android:windowSoftInputMode=”stateAlwaysHidden”>
<-intent-filter>
<-action android:name=”android.intent.action.MAIN” />
<-category android:name=”android.intent.category.LAUNCHER” />
<-category android:name=”android.intent.category.APP_CALCULATOR” />
<-/intent-filter>
<-/activity>
<-/application>
<-/manifest>
我们可以看到,这个AndroidManifest.xml 中并没有太多的东西,只有对android:name=”.Calculator”这个是对我们有用的信息,在这里我们要使用的就是,Calculator.java是这个app的入口函数。
- packages/apps/Calculator/src/com/android/calculator2/Calculator.java
我们接着来看下Calculator.java中的内容。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
……………….
}
setContentView(R.layout.activity_calculator)这里引用了布局文件activity_calculator.xml,
<-LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical”>
<-include
layout=”@layout/display”
android:layout_width=”match_parent”
android:layout_height=”wrap_content” />
<-com.android.calculator2.CalculatorPadViewPager
android:id=”@+id/pad_pager”
android:layout_width=”match_parent”
android:layout_height=”0dip”
android:layout_weight=”1”
android:overScrollMode=”never”>
<-LinearLayout
android:layout_width=”match_parent”
android:layout_height=”match_parent”>
<-include layout=”@layout/pad_numeric” />
<-include layout=”@layout/pad