1. 程式人生 > >安卓基礎:Activity基礎、五大布局

安卓基礎:Activity基礎、五大布局

Activity:

Activity是Android系統中的四大元件之一,可以用於顯示View。它是一種可以包含使用者介面的元件,主要用於和使用者進行互動。

Activity是有生命週期的,每個Activity在其生命週期中最多可能會有四種狀態:
這裡寫圖片描述
Activity的生命週期:
這裡寫圖片描述
Activity有七個回撥方法,覆蓋了Activity生命週期的每一個環節:
1.onCreate():在Activity第一次被建立的時候呼叫,在這個方法中完成Activity的初始化操作
2.onStart():這個方法在Activity有不可見變為可見時呼叫
3.onResume():這個方法在Activity準備好和使用者進行互動的時候呼叫,此時的Activity一定位於返回棧的棧頂,並且處於執行狀態。
4.onPause():這個方法在系統準備去啟動或者回復另一個Activity的時候呼叫。通常會在這個方法中將一些消耗CPU的資源釋放掉,以及儲存一些關鍵資料,但這個方法的執行速度一定要快,不然會影響到新的棧頂活動的使用
5.onStop():這個方法在Activity完全不可見的時候呼叫。它和onPause的主要區別在於,如果啟動的新的Activity是一個對話方塊式的,那麼onPause方法會得到執行,而onStop方法並不會執行
6.onDestroy():這個方法在活動被銷燬之前呼叫,之後Activity的狀態為銷燬狀態
7.onRestart():這個方法在Activity由停止狀態變為執行狀態之前呼叫,也就是活動背重新啟動。
Activity生命週期的幾個過程:

1.啟動Activity:系統會先呼叫onCreate方法,然後呼叫onStart方法,最後呼叫onResume,Activity進入執行狀態。
2.當前Activity被其他Activity覆蓋其上或被鎖屏:系統會呼叫onPause方法,暫停當前Activity的執行。
3.當前Activity由被覆蓋狀態回到前臺或解鎖屏:系統會呼叫onResume方法,再次進入執行狀態。

4.當前Activity轉到新的Activity介面或按Home鍵回到主屏,自身退居後臺:系統會先呼叫onPause方法,然後呼叫onStop方法,進入停滯狀態。

5.使用者後退回到此Activity:系統會先呼叫onRestart方法,然後呼叫onStart方法,最後呼叫onResume方法,再次進入執行狀態。

6.當前Activity處於被覆蓋狀態或者後臺不可見狀態,即第2步和第4步,系統記憶體不足,殺死當前Activity,而後使用者退回當前Activity:再次呼叫onCreate方法、onStart方法、onResume方法,進入執行狀態。

7.使用者退出當前Activity:系統先呼叫onPause方法,然後呼叫onStop方法,最後呼叫onDestory方法,結束當前Activity。
例子來觀察Activity生命週期的過程:

//第一個Activity的佈局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:id="@+id/TheFirst"> <TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="啟動第二個介面" android:id="@+id/button2" android:layout_below="@+id/textView" android:layout_toRightOf="@+id/textView" android:layout_marginTop="55dp" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> </RelativeLayout> //第二個介面的佈局 <?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="horizontal" android:id="@+id/TheSecond" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="第1個按鈕" android:id="@+id/button1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="第2個按鈕" android:layout_gravity="center" android:id="@+id/button2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="bottom|right" android:text="第3個按鈕" android:id="@+id/button3" /> </LinearLayout>
//第一個介面的主程式,在Log.d中列印各種方法被呼叫的順序
package com.example.administrator.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("MyActivity","onCreate");
        Button bt = (Button) findViewById(R.id.button2);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("MyActivity","onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("MyActivity","onResume");

    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("MyActivity","onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("MyActivity","onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("MyActivity","onDestroy");
    }
}


//第二個介面的主程式
package com.example.administrator.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

/**
 * Created by Administrator on 2015/8/18.
 */
public class SecondActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_layout);
        Log.d("MyActivity", "SecondActivity onCreate");
        Button bt = (Button) findViewById(R.id.button);


    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("MyActivity", "SecondActivity onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("MyActivity","SecondActivity onResume");

    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("MyActivity","SecondActivity onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("MyActivity","SecondActivity onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("MyActivity","SecondActivity onDestroy");
    }
}
//在mainfest中註冊這兩個Activity

<activity
            android:name="com.example.administrator.myapplication.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>


        </activity>

        <activity android:name="com.example.administrator.myapplication.SecondActivity"
            android:theme="@android:style/Theme.Dialog">//把第二個介面設定成一個對話方塊



        </activity>

當第二個介面不是一對話方塊的形式出現時,也即是第二個介面啟動後,第一個介面變為不可見時的執行結果為:
這裡寫圖片描述

當第二個介面是以一個對話方塊的形式出現時,即第二個介面啟動後,第一個介面仍然可見,執行結果為:
這裡寫圖片描述

局面佈局

1、LinearLayout線性佈局:

orientation:預設是橫向(horizontal)的,margin屬性(離開周圍),gravity(所有的)和LayoutGravity(我在父控制元件中怎樣佈局)

練習:
實現以下佈局
這裡寫圖片描述

//第一個
<?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"
    >
    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />

</LinearLayout>
//第二個
<?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"

    >
    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        />
    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        />

    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

</LinearLayout>
//第三個
<?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"
    android:weightSum="3">

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:orientation="horizontal"
        android:weightSum="3">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:orientation="vertical"
            android:weightSum="3">

            <Button

                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="2" />

        </LinearLayout>

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>


</LinearLayout>
//第四個
<?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"
    android:weightSum="3"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="3">

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent" />
        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>

        </LinearLayout>


    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="3">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:weightSum="3">
            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>

        </LinearLayout>
        <Button
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="match_parent"
            />

    </LinearLayout>

</LinearLayout>

2、RelativeLayout相對佈局:屬性

alignParentLeft、Right、Bottom、Top=true相對於父空間的上下左右

centerHorizontal

+centerVertical=centerInParent相對於父空間的中間

toLeftOf、toRightOf、above、below向對於後面的ID的控制元件的上下左右
alignLeft、
layout_alignBaseLine基準線對齊

舉例說明:

<?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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:text="button_1"
        android:id="@+id/button"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:text="button_2"
        android:id="@+id/button2"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="button_3"
        android:id="@+id/button3"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="button_4"
        android:id="@+id/button4"
        />
    <Button
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Center"
        android:id="@+id/center"

        android:background="@color/material_deep_teal_200" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/center"
        android:text="bt5"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/center"
        android:text="bt6"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/center"
        android:layout_alignLeft="@id/center"

        android:text="bt7"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/center"
        android:layout_alignLeft="@id/center"

        android:text="bt8"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_alignBaseline="@id/center"

        android:text="bt9"
        />

</RelativeLayout>

結果:
這裡寫圖片描述

3、FrameLayout幀佈局:一層一層往上貼

重要屬性(所有佈局都適用):visibility
顯示visible
invisible不顯示,但佔位置,
gone不顯示,不佔位置

<?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">
    <Button
        android:layout_width="150dp"
        android:layout_height="300dp"
        android:text="button_1"

        />
    <Button
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:text="button_2"/>

</FrameLayout>

這裡寫圖片描述

4、TableLayout表格佈局:(不要求掌握,不常用)
stretchColumn
collapseColumn

5、AbsolutelyLayout絕對佈局:直接按畫素走(不推薦使用,因為手機畫素不同)

相關推薦

基礎Activity基礎五大

Activity: Activity是Android系統中的四大元件之一,可以用於顯示View。它是一種可以包含使用者介面的元件,主要用於和使用者進行互動。 Activity是有生命週期的,每個Activity在其生命週期中最多可能會有四種狀態: Ac

Python基礎十六bool 爾值

轉化 int 空字符串 == 布爾值 lang 字符 沒有 lan bool布爾值(判斷) 1. 取值只有True,False 2. bool沒有值操作 3. 轉換問題   str===>int int(str)   int===>

自定義View基礎-繪製點矩形圓形等

為什麼要自定義View?因為我們在開發中,經常有各種各樣的需求,但是原生的控制元件畢竟只能滿足我們常用的需求,所以我們需要根據自身當前的需求來定製我們的View,話不多說,一步一步來吧。 1.建立類: 建立一個類,暫且將這個類命名為CustomV

自定義View基礎座標系

安卓中的座標系 一.螢幕座標系和數學座標系的區別 由於移動裝置一般定義螢幕左上角為座標原點,向右為x軸增大方向,向下為y軸增大方向, 所以在手機螢幕上的座標系與數學中常見的座標系是稍微有點差別的,詳情如下: (PS:其中的∠a 是對應的,注意y軸方向!) 實際螢幕上的預設座標系如下: PS: 假設其中棕

自定義View基礎顏色

簡要介紹安卓中的顏色相關內容,包括顏色的定義,建立顏色的幾種方式,以及顏色的混合模式等。 一.簡單介紹顏色 安卓支援的顏色模式: 顏色模式 備註 ARGB8888 四通道高精度(32位) ARGB4444 四通道低精度(16位) RGB565 螢幕預設模式

基礎知識回顧------Android中的五大

Android中的五大布局 在android中的佈局有五大類,這五種佈局為別為:LinearLayout(線性佈局),FrameLayout(框架佈局),RelativeLayout(相對佈局),TableLayout(表格佈局),AbsoluteLayout

python基礎元組字典深淺拷貝與函數

dictionary python tuple 函數 開發 小生博客:http://xsboke.blog.51cto.com 小生 Q Q:1770058260 -------謝謝您的參考,如有疑問,歡迎交流一、 元

傳感器基礎

ports htm div 有效 type() 0ms 類型 event pan 基礎部分 傳感器類型 動作傳感器 這類傳感器在三個軸(X、Y、Z)上測量加速度和旋轉角度。包括如下幾個傳感器。 加速(accelerometer)傳感器 陀螺儀(gyroscope)傳感器 重

05 java 基礎運算符程序結構

算術運算符 back 關系 關系運算符 數據類型 ase CA 自增 bsp 賦值運算符 : = 三元運算符 : ? 算術運算符 : +、- 、*、/、% 自增自減運算符: ++、-- 關系運算符:>、<、==、>=、<=、!= 邏輯運算符 :&

Scala基礎閉包柯里化隱式轉換和隱式引數

閉包,和js中的閉包一樣,返回值依賴於宣告在函式外部的一個或多個變數,那麼這個函式就是閉包函式。 val i: Int = 20 //函式func的方法體中使用了在func外部定義的變數 那func就是個閉包函式 val func = (x: Int) => x +

內網滲透基礎內網工作組域控概念介紹

一、什麼是內網 區域網(Local Area Network, LAN),又稱內網,是指在某一區域內由多臺計算機互聯成的計算機組。 區域網可以實現檔案管理、應用軟體共享、印表機共享、掃描器共享、工作組內的日程安排、電子郵件和傳真通訊服務等功能。區域網嚴格意

kotlin開發fragment向activity傳遞資料通過handler,設定回撥方法

從activity向fragment傳遞就比較方便了,直接用: fg.arguments = arguments 現在看看怎麼從fragment向activity傳遞資料。 比如說,我們在一個ViewPage裡面設定了若干個fragment,fragment裡面有一個按鈕,提交相關

JAVA基礎強引用軟引用弱引用虛引用

生活 任何傻瓜都能寫出計算機可以理解的程式碼。好的程式設計師能寫出人能讀懂的程式碼。 前言 在JAVA中,開發人員不需要像C開發人員那樣手動去管理記憶體中物件的生命週期,但是如果需要某些物件具備一定的生命週期(當記憶體不足的時候可以回收一些沒有必要的物件,從而規避一些OOM的風險

【JAVA】基礎執行緒IO流如何打包成可安裝 exe 程式

1.執行緒 程序是一個可以獨立執行的程式單位,執行緒就是一個輕量級的程序。 自定義執行緒的方法: 1.繼承Thread類,重寫Thread類中的run方法,建立繼承類物件,呼叫執行緒的start()方法。 2.實現Runnable介面,重寫run方法,建立實現類物件,建立執行緒類物

JAVA中面向物件基礎抽象類初始化塊

本文轉載連線為:http://android.yaohuiji.com/archives/3241 一、抽象類 用 abstract 修飾的類定義,我們稱之為抽象類,抽象類不能被例項化。 用 abstract 修飾的方法,我們稱之為抽象方法,抽象方法不能有方法體。 面向物

Linux基礎預編譯編譯彙編連結

四:預編譯、編譯、彙編、連結 //詳情參考《程式設計師的自我修養》1-6章 //在原文基礎上做補充和修改 原文:https://blog.csdn.net/weixin_40740059/article/details/84075653 如圖:c程式的4G虛擬地址空間劃分: 

自定義View基礎-顏色

顏色 簡要介紹安卓中的顏色相關內容,包括顏色的定義,建立顏色的幾種方式,以及顏色的混合模式等。 一.簡單介紹顏色 安卓支援的顏色模式: 顏色模式 備註 ARGB8888 四通道高精度(32位) ARGB4444 四

Vue基礎元件--slot非同步元件遞迴元件及其他

slot分發內容 為了讓元件可以組合,我們需要一種方式來混合父元件的內容與子元件自己的模板。這個過程被稱為內容分發。Vue中使用特殊的 <slot> 元素作為原始內容的插槽。 問題(編譯作用域) message 應該繫結到父元件的資料,還

Android基礎Activity生命週期細化

一、   細化Activity的生命週期         在進行Android應用開發的時候,需要考慮如何使用Activity的生命週期中的方法使得程式符合使用者的期望且在activity不需要的時候不會導致系統資源的浪費。下面從activity的啟動和銷燬、暫停和恢復、

自定義View基礎-座標系

一.螢幕座標系和數學座標系的區別 由於移動裝置一般定義螢幕左上角為座標原點,向右為x軸增大方向,向下為y軸增大方向, 所以在手機螢幕上的座標系與數學中常見的座標系是稍微有點差別的,詳情如下: (PS:其中的∠a 是對應的,注意y軸方向!) 實際螢幕上的預設座標系如下: