Android開發(fā)學(xué)習(xí)教程(2)
上一篇我們成功運(yùn)行了Hello World,現(xiàn)在我們詳細(xì)來看看項(xiàng)目結(jié)構(gòu)和運(yùn)行過程或者說運(yùn)行流程。
項(xiàng)目結(jié)構(gòu)
1. AndroidManifest.xml
<
manifest
????
xmlns:android
=
"http://schemas.android.com/apk/res/android"
????
package
=
"com.example.myapplication"
>
????
<
application
????????
android:allowBackup
=
"true"
????????
android:icon
=
"@mipmap/ic_launcher"
????????
android:label
=
"@string/app_name"
????????
android:roundIcon
=
"@mipmap/ic_launcher_round"
????????
android:supportsRtl
=
"true"
????????
android:theme
=
"@style/AppTheme"
>
????????
<
activity
????????????
android:name
=
".MainActivity"
????????????
android:exported
=
"true"
????????????
android:label
=
"@string/app_name"
????????????
android:screenOrientation
=
"portrait"
>
????????????
<
intent-filter
>
????????????????
<
action
?android:name
=
"android.intent.action.MAIN"
?></
action
>
????????????????
<
category
?android:name
=
"android.intent.category.LAUNCHER"
?></
category
>
????????????
</
intent-filter
>
????????
</
activity
>
????
</
application
>
</
manifest
>
xmlns:android:定義android命名空間,這樣使得Android中各種標(biāo)準(zhǔn)屬性能在文件中使用,提供了大部分元素中的數(shù)據(jù)。
package:指定本應(yīng)用內(nèi)java主程序包的包名,它也是一個(gè)應(yīng)用進(jìn)程的默認(rèn)名稱。
application:一個(gè)AndroidManifest.xml中必須含有一個(gè)Application標(biāo)簽,這個(gè)標(biāo)簽聲明了每一個(gè)應(yīng)用程序的組件及其屬性(如icon、label、permission等)。
allowBackup:當(dāng)allowBackup標(biāo)志為true時(shí),用戶即可通過adb backup和adb restore來進(jìn)行對應(yīng)用數(shù)據(jù)的備份和恢復(fù),這可能會(huì)帶來一定的安全風(fēng)險(xiǎn)。
icon:這個(gè)很簡單,就是聲明整個(gè)APP的圖標(biāo),圖片一般都放在drawable文件夾下。
roundIcon:同上,唯一區(qū)別它是圓形的圖標(biāo),如果配置了此項(xiàng),APP安裝在Android 7.1.1以上的手機(jī)顯示的就是圓形圖標(biāo)。
label:聲明整個(gè)APP的名字,字符串常量一般都放在values文件夾下的strings.xml里。
supportsRtl:支持從右往左顯示的布局(正常布局在鏡子里面看到的左右對調(diào)過的樣子)。
theme:是一個(gè)資源的風(fēng)格,它定義了一個(gè)默認(rèn)的主題風(fēng)格給所有的activity,當(dāng)然也可以在自己的theme里面去設(shè)置它,有點(diǎn)類似style。
activity:定義APP中的一個(gè)組件Activity。
name:該Activity的名字。
screenOrientation:該Activity的屏幕方向。
intent-filter:廣播過濾器,后續(xù)會(huì)講到。
action android:name:指定程序入口Activity,在這里是MainActivity。
category android:name:指定當(dāng)前動(dòng)作(Action)被執(zhí)行的環(huán)境。這里的CATEGORY_LAUNCHER決定應(yīng)用程序是否顯示在程序列表里。
2. java文件夾
該文件夾是放項(xiàng)目的源代碼。我們來看MainActivity.java的源碼
public
?class
?MainActivity?
extends
?AppCompatActivity {
????
@Override
????
protected
?void
?onCreate(
@Nullable
?Bundle savedInstanceState) {
????????
super
.onCreate(savedInstanceState);
????????
setContentView(R.layout.act_main);
????
}
}
這里我們暫時(shí)只看第⑤行,這一句的意思就是將布局文件act_main設(shè)置為該Activity的視圖,也就是說MainActivity打開后顯示的頁面就是act_main。
3. res文件夾
程序資源目錄。包含三個(gè)子目錄:drawabel、layout、values。
drawabel:程序里所有的圖片、形狀。
layout:每個(gè)Activity界面對應(yīng)的布局文件。我們來看看MainActivity的布局文件act_main.xml:
<?
xml
?version
=
"1.0"
?encoding
=
"utf-8"
?>
<
LinearLayout
?xmlns:android
=
"http://schemas.android.com/apk/res/android"
????
android:layout_width
=
"match_parent"
????
android:layout_height
=
"match_parent"
????
android:gravity
=
"center"
>
????
<
TextView
????????
android:layout_width
=
"wrap_content"
????????
android:layout_height
=
"wrap_content"
????????
android:text
=
"Hello World"
?/>
</
LinearLayout
>
這里暫時(shí)只看第7行到第11行,可以理解為一個(gè)顯示Hello World的標(biāo)簽。
values:程序中的文字strings.xml、大小dimens.xml、顏色colors.xml、風(fēng)格styles.xml等。例如上面act_main布局文件中的Hello World文字就可以定義在strings.xml中,如下:
<
resources
>
????
<
string
?name
=
"text_hello"
>Hello World</
string
>
</
resources
>
文字如果定義在了strings.xml中,那么布局文件中顯示文字時(shí)使用@string/后面跟上文字對應(yīng)的name來表示,如下:
<
TextView
????
android:layout_width
=
"wrap_content"
????
android:layout_height
=
"wrap_content"
????
android:text
=
"@string/text_hello"
?/>
再比如我們還可以設(shè)置文字的顏色為紅色,我們可以這樣設(shè)置,在colors.xml(如果沒有可以新建一個(gè)文件)中定義:
<?
xml
?version
=
"1.0"
?encoding
=
"utf-8"
?>
<
resources
>
????
<
color
?name
=
"tv_red"
>#ff0000</
color
>
</
resources
>
然后在布局文件中設(shè)置文字的顏色為@color/后面跟上顏色對應(yīng)的name,如下:
<
TextView
????
android:layout_width
=
"wrap_content"
????
android:layout_height
=
"wrap_content"
????
android:text
=
"@string/text_hello"
????
android:textColor
=
"@color/tv_red"
?/>
4. Gradle Scripts文件
這個(gè)文件夾下面都是項(xiàng)目編譯和構(gòu)建的一些配置,讀者可以暫時(shí)跳過這些配置,下一篇章會(huì)著重解釋。
運(yùn)行過程/運(yùn)行流程
Android程序會(huì)先去加載AndroidMainfest.xml文件,接著去找到application標(biāo)簽下intent-filter action android:name=”android.intent.action.MAIN”的Activity作為第一個(gè)Activity啟動(dòng),啟動(dòng)后會(huì)先調(diào)用Activity的onCreate函數(shù),而我們在此函數(shù)中通過setContentView設(shè)置了視圖act_main,所以最終我們在手機(jī)屏幕上看到的是MainActivity頁面上顯示的Hello World文字。
原文連接:https://yunjunet.cn/876692.html