跟我學android-Android應用結構分析(四),android應用開發

來源:互聯網
上載者:User

跟我學android-Android應用結構分析(四),android應用開發

自動產生的R.java檔案說明

 

 1 public final class R { 2     public static final class attr { 3     } 4     public static final class dimen { 5         /**  Default screen margins, per the Android Design guidelines.  6  7          Customize dimensions originally defined in res/values/dimens.xml (such as 8          screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here. 9     10          */11         public static final int activity_horizontal_margin=0x7f040000;12         public static final int activity_vertical_margin=0x7f040001;13     }14     public static final class drawable {15         public static final int ic_launcher=0x7f020000;16     }17     public static final class id {18         public static final int action_settings=0x7f080002;19         public static final int btn_change=0x7f080001;20         public static final int tv_info=0x7f080000;21     }22     public static final class layout {23         public static final int activity_main=0x7f030000;24         public static final int second_layout=0x7f030001;25     }

 

通過R類中的注釋可以看出,R.java是由aapt工具根據應用中的資源檔自動產生的,因此,我們可以把R.java看成一個Android應用的資源字典檔案

apt產生R.java檔案的規則主要是如下2點:

  • 每類資源對應R類的一個內部類。
  • 每個具體的資源對應內部類的一個屬性。

所以大家請記住,在res裡的命名一定要規範,因為他會在R檔案裡產生對應的屬性名稱,要符合java命名規範

res目錄說明

 

res 是resource的縮寫,該目錄存放了Android應用所用的全部資源,包括 圖片,布局,字串資源,顏色資源,尺寸資源等。

Android按照約定,將不同的資源放在不同的檔案夾內,這樣可以方便aapt工具掃描這些資源,並為他們產生對應的資源清單 R.java

我們開啟res/values/strings.xml檔案,可以看到該檔案的內容非常的簡單

 

1 <?xml version="1.0" encoding="utf-8"?>2 <resources>3 4     <string name="app_name">FirstAndroid</string>5     <string name="action_settings">Settings</string>6     <string name="hello_world">Hello world!</string>7 8 </resources>

這個資源檔定義了3個字串常量,app_name 是一個常量名稱,FirstAndroid是常量值。一旦定義了這份資源檔後,Android項目允許分別在java代碼、XML代碼中使用這份資源檔中定義的字串資源。

        ♦   在Java代碼中使用資源

    為了能在Java代碼裡使用資源,aapt會為Android項目產生一個R.java檔案,R類裡為每份資源定義了一個內部類,其中每個資源對應內部類裡的一個int類型的屬性。例如的strings.xml資源檔,在R檔案裡產生的內容如下

    

1  public static final class string {2         public static final int action_settings=0x7f050001;3         public static final int app_name=0x7f050000;4         public static final int hello_world=0x7f050002;5     }

    藉助R類,在Java代碼中 我們就可以通過 R.string.app_name來引用” FirstAndroid”字串常量

       ♦   在XML代碼中使用資源

         在XML裡引用資源更簡單,只要按照如下格式來訪問即可

        @<資源對應的內部類的類名>/<資源的名稱>

      例如 我現在想讓activity_main.xml 這個布局的文本顯示我們的app_name

      則 修改 activity_main.xml 中 TextView 的屬性即可

 assets目錄說明

   原生資源檔夾,在這裡的檔案不會被aapt產生資源到R檔案中去

 libs目錄說明

   庫檔案夾,一般存放jar,so檔案

 AndroidManifest.xml檔案說明

AndroidManifest.xml 資訊清單檔是每個Android應用程式必須的,它是整個應用程式的全域描述檔案,AndroidManifest.xml 資訊清單檔說明了該應用的名稱,表徵圖,以及包含的組件等

Androidmanifest資訊清單檔通常包含如下資訊:

  應用程式的包名,該包名作為應用的唯一標識。

  應用程式套件組合含的組件(activity,service等)

  應用程式相容的最低版本

  應用程式使用系統所需的許可權聲明

下面是一份簡單的Androidmanifest資訊清單檔

 

 1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3     package="com.zy.android.firstandroid" 4     android:versionCode="1" 5     android:versionName="1.0" > 6  7     <uses-sdk 8         android:minSdkVersion="8" 9         android:targetSdkVersion="17" />10 11     <application12         android:allowBackup="true"13         android:icon="@drawable/ic_launcher"14         android:label="@string/app_name"15         android:theme="@style/AppTheme" >16         <activity17             android:name="com.zy.android.firstandroid.MainActivity"18             android:label="@string/app_name" >19             <intent-filter>20                 <action android:name="android.intent.action.MAIN" />21 22                 <category android:name="android.intent.category.LAUNCHER" />23             </intent-filter>24         </activity>25         </application>26 27 </manifest>

 許可權說明

  一個Android應用可能需要許可權才能調用Android系統的功能

  聲明運行該應用本身需要的許可權

  通過為<manifest …/>元素添加<uses-permission…/>子項目即可為本應用聲明許可權。

  例如我們的程式需要訪問網路,則需要聲明網路許可權

      <!-- 訪問網路許可權 -->
      <uses-permission android:name="android.permission.INTERNET"/>

其實許可權的用法並不難,大家可以在官網上尋找到 android的所有需要許可權。這裡就不給大家列出來了。

 

 


Android應用開發,下方四個按鈕切換是怎實現的

反編譯了5.0 main_tab.xml的布局
<?xml version="1.0" encoding="utf-8"?><TabHost android:id="@id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android=" <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0.0dip" android:layout_weight="1.0" /> <TabWidget android:id="@android:id/tabs" android:visibility="gone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.0" /> <LinearLayout android:gravity="bottom" android:layout_gravity="bottom" android:orientation="horizontal" android:id="@id/main_tab_group" android:background="@drawable/mmfooter_bg" android:paddingTop="2.0dip" android:layout_width="fill_parent" android:layout_height="wrap_content"> <FrameLayout android:background="@null" android:layout_width="0.0dip" android:layout_height=&......餘下全文>>
 
Android應用開發解密第四章4-5例子崩潰,大蝦賜教,代碼如下,log看

如果你要訪問連絡人,加上:
# <!-- 讀取連絡人許可權 -->
# <uses-permission android:name="android.permission.READ_CONTACTS"/>

如果你要打電話,加上
# <!-- 撥打到電話許可權 -->
# <uses-permission android:name="android.permission.CALL_PHONE"/>

很明顯 現在你要讀取連絡人資訊。AndroidManifest.xml uses-permission 加上
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.