Android 控件開(kāi)發(fā)筆記(一)

pic.png

1、 什么是Activity

  Activity是一個(gè)應(yīng)用程序組件,為應(yīng)用程序提供一個(gè)可視化的界面,用戶通過(guò)此界面與應(yīng)用程序交互。

2、 介紹一下Mainifest 、 MainActivity 、Layout

  • Mainifest.xml 應(yīng)用程序的主配置文件

  • MainActivity 主界面

  • Layout 布局文件

3、 Activity和Layout之間的關(guān)系

Activity通過(guò)調(diào)用findViewById()函數(shù),引用頁(yè)面布局的控件,將Activity與控件聯(lián)系起來(lái),通常括號(hào)中的是(R.id.xxx),xxx為頁(yè)面布局中定義的控件ID名。此時(shí),我們還要解釋一下R,R.java文件是由aapt工具根據(jù)應(yīng)用中的資源文件來(lái)自動(dòng)生成的,因此我們可以吧R.java看成Android應(yīng)用的資源字典,aapt生成R.java文件的規(guī)則主要是兩條:每類資源對(duì)應(yīng)R類的一個(gè)內(nèi)部類。比如,所有界面布局資源對(duì)應(yīng)于layout內(nèi)部類;每個(gè)具體的資源項(xiàng)對(duì)應(yīng)于內(nèi)部類的一個(gè)public static final int類型的Field,若界面布局中用到了ok,show兩個(gè)標(biāo)識(shí)符,則R.id類里就包含了這兩個(gè)Field。隨著我們不斷地向Android項(xiàng)目中添加資源,R.java文件的內(nèi)容也會(huì)越來(lái)越多。

Activity和View之間的關(guān)系

在Activity onCreate方法中初始化了View 的時(shí)候, 調(diào)用了View 的onFinishInflate
在執(zhí)行完 Activity的 onResume 方法之后,才真正開(kāi)始了View的繪制工作:onMeasure --> onSizeChanged --> onLayout --> onDrawonMeasure,onSizeChanged,onLayout,onDraw可能由于setVisible或onresume調(diào)用多次,而onAttachedToWindow與onDetachedFromWindow在創(chuàng)建與銷毀view的過(guò)程中只會(huì)調(diào)用一次
Activity執(zhí)行onDestroy后,View執(zhí)行onDetackedFromWindow

View 的生命周期為:
[改變可見(jiàn)性] --> 構(gòu)造View --> onFinishInflate --> onAttachedToWindow --> onMeasure --> onSizeChanged --> onLayout --> onDraw -->
onDetackedFromWindow

4、什么時(shí)候在Layout中更改屬性?什么時(shí)候在代碼中修改屬性

Activity通過(guò)調(diào)用findViewById()函數(shù),引用頁(yè)面布局的控件并進(jìn)行修改

textView = (TextView)findViewById(R.id.MainTextView);
textView.setTextColor(2);
textView.setText(R.string.specialName);
System.out.print("是否獲取成功: " + textView+ "");

5、 什么是View

 在屏幕方塊中顯示的內(nèi)容

6、 為View添加監(jiān)聽(tīng)器

 監(jiān)聽(tīng)器是一個(gè)對(duì)象,用于捕獲View的狀態(tài),當(dāng)View的狀態(tài)發(fā)生更改時(shí),可使用監(jiān)聽(tīng)器對(duì)其進(jìn)行相應(yīng)的操作。同一個(gè)控件可以設(shè)置多種監(jiān)聽(tīng)器,對(duì)不同狀態(tài)進(jìn)行捕獲監(jiān)聽(tīng)。

操作步驟:

/**
 * 1、布局文件中定義控件
 * 2、Activity中獲取代表控件的對(duì)象 button = (Button)findViewById(R.id.button)
 * 3、定義一個(gè)類實(shí)現(xiàn)監(jiān)聽(tīng)器接口,并重寫(xiě)其抽象方法  class ButtonListener implements View.OnClickListener  public void onClick
 */
class ButtonListener implements View.OnClickListener{
        @Override
        public void onClick(View view) {
            //當(dāng)控件被點(diǎn)擊之后調(diào)用
            sum ++ ;
            System.out.print("當(dāng)前點(diǎn)擊次數(shù)為: " + sum);
        }
    }
    
 button = (Button)findViewById(R.id.button);
 buttonListener = new ButtonListener();
 button.setOnClickListener(buttonListener);

Layout與ViewGroup之間的關(guān)系

/**
 * Layout與ViewGroup之間的關(guān)系
 * - ViewGroup是一個(gè)容器,而這個(gè)容器是繼承自View的
 * - ViewGroup是一個(gè)基類,并且是Layout和一些組件的基類
 * - Linear Layout 線性布局       Relative Layout 相對(duì)布局 開(kāi)發(fā)中用的是最多的
 * - AdapterView(List View , Gird View) 已經(jīng)封裝好的布局視圖
 */


/**
 * match_parent 會(huì)填滿父控件   fill_parent 在SDK2.0以下需要使用,普遍針對(duì)4.0以上的  warp_parent 包裹內(nèi)容,由內(nèi)容決定的  orientation排放的方向
 * - Gravity
 * - Android:gravity 用于設(shè)置該控件中內(nèi)容相對(duì)于該控件的對(duì)齊方式
 * - Android:layout_gravity: 用于設(shè)置該控件相對(duì)于父控件的對(duì)齊方式
 * - Padding:用于設(shè)置該控件中內(nèi)容相對(duì)于該控件的邊距,即內(nèi)邊距
 * - margin: 用于設(shè)置該控件相對(duì)于其他控件的邊距,即外邊距
 * - weight: layout_weight的值用于在線性布局中指定父控件剩余空間的分配比例。
 * - 所有的視圖都有一個(gè)layout_weight值,默認(rèn)為零,意思是需要顯示 多大的視圖就占據(jù)多大的屏幕空間。若賦一個(gè)高于零的值,則將父視 圖中的可用空間分割,分割大小具體取決于每一個(gè)視圖的layout_weight 值以及該值在當(dāng)前屏幕布局的整體layout_weight值和在其它視圖屏幕布 局的layout_weight值中所占的比率而定。 
 * 舉個(gè)例子:比如說(shuō)我們?cè)?水平方向上有一個(gè)文本標(biāo)簽和兩個(gè)文本編輯元素. 該文本標(biāo)簽并無(wú)指定layout_weight值,所以它將占據(jù)需要提供的最少空間。 如果兩個(gè)文本編輯元素每一個(gè)的layout_weight值都設(shè)置為1,則兩者平分  在父視圖布局剩余的寬度(因?yàn)槲覀兟暶鬟@兩者的重要度相等)。如果兩個(gè)文本編輯元素其中第一個(gè)的layout_weight值設(shè)置為1,而第二個(gè)的設(shè)置為2, 則剩余空間的三分之二分給第一個(gè),三分之一分給第二個(gè)(數(shù)值越小,重要度越高)。
 */


  /**
 * - Andorid中像素單位  px dp sp
 * - px:像素,全稱圖像元素,分辨率的尺寸單位
 * - dpi:(dot per inch) 一般是指每英寸的像素,類似于密度,即每英寸圖片上的像素點(diǎn)數(shù)量,用來(lái)表示圖片的清晰度
 * - dp:dip(Device Independent pixels),設(shè)備獨(dú)立像素
 * - sp:距離單位 -可縮放像素,一般用于指定字體大小。-當(dāng)用戶修改手機(jī)系統(tǒng)字體時(shí),應(yīng)用程序中的字體大小會(huì)隨之變化。
 */
Screenshot_1507690638-w540
Screenshot_1507690638-w540

布局代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#6B8E23"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="東方網(wǎng)絡(luò)金融"
            android:layout_marginRight="10dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="信息服務(wù)部" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#ffb6c1"
        android:layout_weight="1"
        android:orientation="vertical"
        android:gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="東方網(wǎng)絡(luò)金融"
            android:layout_marginBottom="10dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="信息服務(wù)部" />
        
    </LinearLayout>

</LinearLayout>

如何定義一個(gè)Activity

  • 創(chuàng)建一個(gè)Activity子類

  • 重寫(xiě)Activity中的onCreate方法

  • 在AndroidManifest.xml中對(duì)Activity進(jìn)行注冊(cè)

啟動(dòng)一個(gè)Activity

  • 創(chuàng)建Intent意圖對(duì)象

  • 調(diào)用setClass 設(shè)置要啟動(dòng)哪個(gè)Activity

  • 調(diào)用startActivity進(jìn)行跳轉(zhuǎn)

private Button button;
private  int sum = 0;
private ButtonListener buttonListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //為當(dāng)前的Activity設(shè)置當(dāng)前顯示的內(nèi)容
    setContentView(R.layout.activity_main);

    //為button添加監(jiān)聽(tīng)器
    button = (Button)findViewById(R.id.button);
    buttonListener = new ButtonListener();
    button.setOnClickListener(buttonListener);
}

class ButtonListener implements View.OnClickListener{
    @Override
    public void onClick(View view) {
        //當(dāng)控件被點(diǎn)擊之后調(diào)用
        sum++ ;
        System.out.print("當(dāng)前點(diǎn)擊次數(shù)為:" + sum);

        Intent intent = new Intent();
        intent.setClass(MainActivity.this,OtherActivity.class);
        startActivity(intent);
    }
}

基礎(chǔ)控件使用方法

TextView適用條件

  • 當(dāng)文本內(nèi)容為電話、郵箱或者特定格式時(shí)
  • 當(dāng)文本內(nèi)容超出控件寬度時(shí)
  • 當(dāng)需要更改文本字體類型和風(fēng)格時(shí)

color:

1、導(dǎo)入color.xml文件至values文件夾下。
2、xml布局文件中,通過(guò)@color/對(duì)應(yīng)顏色名稱

drawable:

1、導(dǎo)入圖片資源(.jpg/png)至drawable文件夾下(考慮適配的話,需要多張圖片。不考慮的話,一張足矣)
2、xml布局文件中,通過(guò)@drawable/圖片名稱

string:

1、values文件夾下的strings.xml文件下,定義鍵值對(duì)。key:內(nèi)容名 values:對(duì)應(yīng)文字內(nèi)容
2、xml布局文件中,通過(guò)@string/內(nèi)容名(國(guó)際化、使xml布局文件更加整潔)

sp/dp:

實(shí)際開(kāi)發(fā)中,有時(shí)為了美觀、一致性 會(huì)使用dp來(lái)作為文字大小的單位。官方推薦使用sp。

autoLink:

設(shè)置特殊文字格式(web/phone/email/..)

ellipsize:

start/end/middle(一定要是singleline) 為顯示不下的內(nèi)容添加省略號(hào)

跑馬燈:

  • ellipsize:marquee
  • singeline
  • textIsSelectable
  • marqueeRepeatLimit:forever..
  • typeface:設(shè)置各種文字類型
  • textSytle:設(shè)置文字風(fēng)格
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/textview01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView基本屬性解析" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/darkorange"
        android:text="帶有顏色的TextView" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/inter"
        android:text="帶有圖片背景的TextView" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/more" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="設(shè)置文字大小、顏色"
        android:textColor="@color/royalblue"
        android:textSize="15sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:autoLink="web"
        android:text="官網(wǎng)地址:http://www.dfjr888.com" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:autoLink="email"
        android:text="郵箱:xxxxxxxxx@163.com" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:autoLink="phone"
        android:text="電話:1234567980" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:autoLink="map"
        android:text="經(jīng)緯度暫時(shí)無(wú)法使用,google地圖已經(jīng)退出啦。期待回歸!" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableBottom="@drawable/ic_launcher"
        android:drawableLeft="@drawable/ic_launcher"
        android:drawableRight="@drawable/ic_launcher"
        android:drawableTop="@drawable/ic_launcher"
        android:gravity="center"
        android:text="在我的周圍添加圖片吧!" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/magenta"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:singleLine="true"
        android:text="@string/more"
        android:textColor="@color/wheat"
        android:textIsSelectable="true" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="設(shè)置字體風(fēng)格"
        android:textStyle="bold|italic" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="設(shè)置文字字體類型"
        android:typeface="monospace" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#6B8E23"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="東方網(wǎng)絡(luò)金融"
            android:layout_marginRight="10dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="信息服務(wù)部" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#ffb6c1"
        android:layout_weight="1"
        android:orientation="vertical"
        android:gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="東方網(wǎng)絡(luò)金融"
            android:layout_marginBottom="10dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="信息服務(wù)部" />
        
    </LinearLayout>

</LinearLayout>
Screenshot_1507710397
Screenshot_1507710397
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,008評(píng)論 25 709
  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線程,因...
    小菜c閱讀 7,325評(píng)論 0 17
  • 原文地址:http://www.android100.org/html/201606/06/241682.html...
    AFinalStone閱讀 1,291評(píng)論 0 1
  • Day1: 在代碼中通過(guò)R.string.hello_world可以獲得該字符串的引用; 在XML中通過(guò)@stri...
    冰凝雪國(guó)閱讀 1,628評(píng)論 0 5
  • 初秋的一天,受邀去一對(duì)老人家做客。老人的家在浙江中部的一個(gè)村子里,那是一個(gè)典型的江南村落,依山傍水,房屋錯(cuò)落有致,...
    淡泊人生的智慧閱讀 399評(píng)論 0 0

友情鏈接更多精彩內(nèi)容