Android基礎學習【曆史流程重走】 ---- Android入門基礎(一)

來源:互聯網
上載者:User

標籤:

一、曆史回顧

隨科技的迅速發展,當前已經全線進入4G時代,5G時代也即將開啟。Android版本迭代迅速,如今已是6.0的版本。時不時可以看到,手機危害了當前人群的生活,如熬夜看手機會深度傷害眼睛,上班族路上低頭黨,聚會吃飯外只剩手機黨等。確實存在一些情形,但是卻無不在強調在當今社會手機在人的生活中,佔有越來越重的地位。手機通訊,到手機娛樂、辦公,以至於到手機管理自身財富。甚至於將來,手機將管理我們的車、房。技術的潮流不可阻擋,最好是做一個弄潮兒,次之可以“隨波逐流”。聊了很多題外話,就是想說,做移動開發的小夥伴,你們太有眼光了!

重走Android重生路,一切才剛剛開始~_~


二、Android體繫結構、虛擬機器


Kernel作為核心,驅動硬體,驅動硬體實現最終目的效果;對核心的驅動操作,封裝成為庫檔案,形成Library;Application就是一個應用,系統應用有打電話、發簡訊、照相等,實際的開發應用有工具、商城、遊戲類等;Application Framework構成針對Android應用的頂層管理。包括Activity Manager,Window Manager 等。

相比於JVM,Dalvik虛擬機器有更多的優勢,將所有頭部組合,抽離出來常量池,將方法分門別類,實現所有java的整理。理解協助:將一個檔案壓縮成為壓縮包後拷貝,提升資料轉送速度。


之後又推出ART虛擬機器。java作為進階語言,在機器執行命令之前需要編譯。開啟ART虛擬機器,在程式安裝時就直接將程式翻譯成為機器語言。從而實現代碼命令直接執行,從而提升效率。但是佔用記憶體大、運行耗能多、內部依舊不穩定,阻礙了ART的快速擴充。


三、Android開發環境

ADT是使用率最高的環境。最近,隨著Android Studio的逐漸成熟,ADT的使用,正在逐漸被替換掉。Google公司不提供後續維護,是最大的原因。如下是Eclipse環境下的項目目錄結構:


在AS下使用Project模式,項目目錄結構相似。AS更大的優勢在於,內部嵌套gradle,能夠實現自動打包、多版本、多渠道打包。

ADB即是Android Debug Bridge,用於串連開發環境和運行環境。adb命令可以方便使用。常用adb命令:

<span style="font-size:18px;">adb  kill-server :殺死服務,中斷連線adb  start-server :開啟服務,串連裝置                                                       【adb install XXX.apk :安裝手機軟體   安裝不可用】adb  devices  :重啟服務,連結裝置adb  connect 127.0.0.1:6555  連結天天模擬器  adb  connect 127.0.0.1:62001  連結夜神模擬器adb uninstall  包名:卸載手機軟體   卸載可用  Adb shell  進入裝置Adb shell input keyevent BACK按鍵Adb shell input tap X Y 點擊座標點Adb shell input swipe X Y X Y滑動adb shell dumpsys activity  [ activities ]   查看activity   [] 可選adb    ps 是看進程的adb    top命令是看佔用率的     查看手機CPU佔用率                 7817  0  15% R    37 596756K  72764K  fg u0_a75   com.ds365.order.test  //Monkey運行過程中                 8599  0   3% S     34 586604K  56884K  fg u0_a75    com.ds365.order.test   //運行                 8599  0   0% S     30 573724K  55252K  bg u0_a75   com.ds365.order.test   //後台運行</span>
在開發環境中,還提供了一些工具,如DDMS,hierarchyviewer。

DDMS可以管理虛擬機器。

hierarchyviewer可用於尋找View id,弄清楚View之間的相互關係。當前工具對於MonkeyRunner自動化測試提供很好的協助。


四、一個小程式

<span style="font-size:18px;"><span style="font-size:18px;">public class MainActivity extends AppCompatActivity {    private Button phoneCall;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        phoneCall = (Button) findViewById(R.id.phone_call);        phoneCall.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                /**                 * 測試發送訊息                 */                SmsManager smsManager = SmsManager.getDefault();                smsManager.sendTextMessage("15510728213", "moniqi", "你好", null, null);                /**                 * 指定意圖:建立對象,設定動作,攜帶資料,觸發動作                 */                Intent intent = new Intent();                intent.setAction(Intent.ACTION_CALL);                intent.setData(Uri.parse("tel://15510728213"));                startActivity(intent);            }        });    }    /**     * 跳轉下一介面     *     * @param view     */    public void changeNextPage(View view) {        startActivity(new Intent(MainActivity.this,OnClickEventActivity.class));    }}</span></span>
基本編程流程:修改編寫xml檔案,寫主類:找到關心控制項,為控制項添加事件。
點擊事件的四種寫法:

<span style="font-size:18px;">public class OnClickEventActivity extends Activity implements View.OnClickListener {    private Button clickSecond;    private Button clickThird;    private Button clickFour;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.onclick_event_layout);        clickSecond = (Button) findViewById(R.id.onclick_second);        clickThird = (Button) findViewById(R.id.onclick_third);        clickFour = (Button) findViewById(R.id.onclick_four);        clickSecond.setOnClickListener(new ClickSecond());        clickThird.setOnClickListener(new View.OnClickListener() {            /**             * 第三種方法:匿名內部類實現點擊方法             */            @Override            public void onClick(View v) {                Toast.makeText(OnClickEventActivity.this, "匿名內部類實現", Toast.LENGTH_SHORT).show();            }        });        clickFour.setOnClickListener(this);    }    /**     * 第一種方式:xml中寫方法     *     * @param view     */    public void clickEventFirst(View view) {        Toast.makeText(OnClickEventActivity.this, "xml中寫方法", Toast.LENGTH_SHORT).show();    }    /**     * 第四種方法:類實現Onclick介面     */    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.onclick_four:                Toast.makeText(OnClickEventActivity.this, "類實現Onclick介面", Toast.LENGTH_SHORT).show();                break;        }    }    /**     * 第二種方法:內部類實現點擊方法     */    class ClickSecond implements View.OnClickListener {        @Override        public void onClick(View v) {            Toast.makeText(OnClickEventActivity.this, "內部類實現", Toast.LENGTH_SHORT).show();        }    }}</span>

五、四大布局

四大布局是:RelativeLayout、LinearLayout、FrameLayout、TableLayout。絕對布局不建議使用。

相對布局(RelativeLayout):

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:id="@+id/middle"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="中間" />    <Button        android:id="@+id/left"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerVertical="true"        android:layout_toLeftOf="@+id/middle"        android:text="左" />    <Button        android:id="@+id/right"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerVertical="true"        android:layout_toRightOf="@+id/middle"        android:text="右" />    <Button        android:id="@+id/up"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_above="@+id/middle"        android:layout_centerHorizontal="true"        android:text="上" />    <Button        android:id="@+id/down"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/middle"        android:layout_centerHorizontal="true"        android:text="下" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="左上" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:text="右上" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:text="左下" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_alignParentRight="true"        android:text="右下" /></RelativeLayout></span>


線性布局(LinearLayout):

<span style="font-size:18px;"><?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:orientation="vertical">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="4dp"            android:text="LinearLayout第一行第一列" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="4dp"            android:text="LinearLayout第一行第二列" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <TextView            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_margin="4dp"            android:layout_weight="1"            android:text="LinearLayout第二行第一列" />        <TextView            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_margin="4dp"            android:layout_weight="1"            android:text="LinearLayout第二行第二列" />    </LinearLayout></LinearLayout></span>

幀布局(FrameLayout):

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:layout_width="350dp"        android:layout_height="350dp"        android:background="@color/black"        android:layout_gravity="center"/>    <TextView        android:layout_width="300dp"        android:layout_height="300dp"        android:background="@color/blue"        android:layout_gravity="center"/>    <TextView        android:layout_width="260dp"        android:layout_height="260dp"        android:background="@color/red"        android:layout_gravity="center"/>    <TextView        android:layout_width="200dp"        android:layout_height="200dp"        android:background="@color/green"        android:layout_gravity="center"/>    <TextView        android:layout_width="100dp"        android:layout_height="100dp"        android:background="@color/colorAccent"        android:layout_gravity="center"/></FrameLayout></span>

RelativeLayout屬性說明:

   第一類:屬性值為true或false 

    android:layout_centerHrizontal  水平置中 
    android:layout_centerVertical   垂直置中 
    android:layout_centerInparent    相對於父元素完全置中 
    android:layout_alignParentBottom 貼緊父元素的下邊緣 
    android:layout_alignParentLeft   貼緊父元素的左邊緣 
    android:layout_alignParentRight  貼緊父元素的右邊緣 
    android:layout_alignParentTop    貼緊父元素的上邊緣 
    android:layout_alignWithParentIfMissing  如果對應的兄弟元素找不到的話就以父元素做參照物 

    第二類:屬性值必須為id的引用名“@id/id-name” 
    android:layout_below      在某元素的下方 
    android:layout_above      在某元素的的上方 
    android:layout_toLeftOf   在某元素的左邊 
    android:layout_toRightOf  在某元素的右邊 

    android:layout_alignTop   本元素的上邊緣和某元素的的上邊緣對齊 
    android:layout_alignLeft  本元素的左邊緣和某元素的的左邊緣對齊 
    android:layout_alignBottom 本元素的下邊緣和某元素的的下邊緣對齊 
    android:layout_alignRight  本元素的右邊緣和某元素的的右邊緣對齊 

    第三類:屬性值為具體的像素值,如30dip,40px 
    android:layout_marginBottom              離某元素底邊緣的距離 
    android:layout_marginLeft                   離某元素左邊緣的距離 
    android:layout_marginRight                 離某元素右邊緣的距離 
    android:layout_marginTop                   離某元素上邊緣的距離 

android:gravity  
android:gravity屬性是對該view 內容的限定.比如一個button 上面的text.  你可以設定該text 在view的靠左,靠右等位置.以button為例,android:gravity="right"則button上面的文字靠右 


android:layout_gravity 
android:layout_gravity是用來設定該view相對與起父view 的位置.比如一個button 在linearlayout裡,你想把該button放在靠左、靠右等位置就可以通過該屬性設定.以button為例,android:layout_gravity="right"則button靠右 


EditText 的 android:hint 
設定EditText為空白時輸入框內的提示資訊。 


ImageView 的 android:scaleType: 
android:scaleType是控製圖片如何resized/moved來匹對ImageView的size。ImageView.ScaleType / android:scaleType值的意義區別: 


CENTER /center  按圖片的原來size置中顯示,當圖片長/寬超過View的長/寬,則截取圖片的置中部分顯示 
CENTER_CROP / centerCrop  按比例擴大圖片的size置中顯示,使得圖片長(寬)等於或大於View的長(寬) 
CENTER_INSIDE / centerInside  將圖片的內容完整置中顯示,通過按比例縮小或原來的size使得圖片長/寬等於或小於View的長/寬 
FIT_CENTER / fitCenter  把圖片按比例擴大/縮小到View的寬度,置中顯示 
FIT_END / fitEnd   把圖片按比例擴大/縮小到View的寬度,顯示在View的下部分位置 
FIT_START / fitStart  把圖片按比例擴大/縮小到View的寬度,顯示在View的上部分位置 
FIT_XY / fitXY  把圖片不按比例擴大/縮小到View的大小顯示 
MATRIX / matrix 用矩陣來繪製,動態縮小放大圖片來顯示。 

源碼下載

雄關漫道真如鐵 而今邁步從頭越~_~        

Android基礎學習【曆史流程重走】 ---- Android入門基礎(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.