Android UI開發基礎

Android UI開發基礎 52html

3.1 Android UI框架 52android

3.1.1 ViewViewGroup 52git

請分析ViewViewGroup的關係。框架

答案:ViewViewGroup的父類。AndroidUI開發使用層次模型來完成,通常都是在一個ViewGroup中嵌套多層ViewGroup,每一層中含有隨意數目的View。咱們能夠將整個屏幕看作一個ViewGroup,它同時也是一個View,而在這個總體的ViewGroup之中,又有多個ViewGroupView,每一個ViewGroup中又能夠有多個子ViewGroupView佈局

3.1.2 Android UI開發分類 53this

3.2 經常使用的基本控件(一) 55spa

3.2.1 TextView 58.net

請說明TextView繼承自誰和它是用來幹什麼的。xml

答案:它直接繼承自View,用來向用戶顯示文本並容許用戶編輯它。htm

請寫出TextView的很多於四個子孫類。

答案:EditTextButtonDigitalClockTextClockCheckedTextViewChronometerCheckBoxRadioButtonSwitch等。

3.2.2 EditText 62

EditText是什麼,及其用處。

答案:EditText繼承自TextView,專門用來獲取用戶輸入的文本信息。因爲它是繼承自TextView的,因此能夠說EditText就是一個可編輯的TextView

3.2.3 Button 63

Button繼承自什麼類,及其做用。

答案: Button一樣繼承自TextView,它自己還有幾個子孫類,包括CheckBoxRadioButtonSwitchToggleButton等。按鈕在UI開發中用的不少,經常使用來響應用戶的點擊。

3.3 Android UI佈局 64

3.3.1 LinearLayout——線性佈局 65

3.3.2 RelativeLayout——相對佈局 67

3.3.3 TableLayout——表格佈局 68

3.3.4 FrameLayout——幀佈局 70

3.3.5 AbsoluteLayout——絕對佈局 71

請寫出Android中的五大布局。

答案:LinearLayout線性佈局、RelativeLayout相對佈局、TableLayout表格佈局、FrameLayout幀佈局和AbsoluteLayout絕對佈局。

請介紹下Android中經常使用的五種佈局

答案:Android佈局是應用界面開發的重要一環,在Android中,共有五種佈局方式,分別是:FrameLayout(框架佈局),LinearLayout (線性佈局),

AbsoluteLayout(絕對佈局),RelativeLayout(相對佈局),TableLayout(表格佈局)。 

  1.FrameLayout    

    這個佈局能夠當作是牆腳堆東西,有一個四方的矩形的左上角牆腳,咱們放了第一個東西,要再放一個,那就在放在原來放的位置的上面,這樣依次的放,會蓋住原來的東西。這個佈局比較簡單,也只能放一點比較簡單的東西。    

2.LinearLayout    

線性佈局,這個東西,從外框上能夠理解爲一個div,他首先是一個一個從上往下羅列在屏幕上。每個LinearLayout裏面又可分爲垂直佈局 android:orientation="vertical")和水平佈局(android:orientation="horizontal" )。當垂直佈局時,每一行就只有一個元素,多個元素依次垂直往下;水平佈局時,只有一行,每個元素依次向右排列。    

linearLayout中有一個重要的屬性 android:layout_weight="1",這個weight在垂直佈局時,表明行距;水平的時候表明列寬;weight值越大就越大。    

3.AbsoluteLayout    

    絕對佈局猶如div指定了absolute屬性,用X,Y座標來指定元素的位置android:layout_x="20px" android:layout_y="12px" 這種佈局方式也比較簡單,可是在垂直隨便切換時,每每會出問題,並且多個元素的時候,計算比較麻煩。    

4.RelativeLayout    

    相對佈局能夠理解爲某一個元素爲參照物,來定位的佈局方式。主要屬性有:    

    相對於某一個元素    

    android:layout_below="@id/aaa" 該元素在 idaaa的下面    

    android:layout_toLeftOf="@id/bbb" 改元素的左邊是bbb    

     相對於父元素的地方    

     android:layout_alignParentLeft="true"  在父元素左對齊    

     android:layout_alignParentRight="true" 在父元素右對齊    

     還能夠指定邊距等,具體詳見API    

            

5.TableLayout    

     表格佈局相似Html裏面的Table。每個TableLayout裏面有表格行TableRowTableRow裏面能夠具體定義每個元素,設定他的對齊方式 android:gravity=""     

     每個佈局都有本身適合的方式,另外,這五個佈局元素能夠相互嵌套應用,作出美觀的界面。

3.4 經常使用的基本控件(二) 71

3.4.1 ImageViewImageButton 72

請寫出ImageButtonImageView的關係和區別。

答案:ImageButton繼承自ImageView ImageButton用在須要接收用戶點擊的圖片顯示中,而ImageView則通常用來直接顯示圖片。

3.4.2 CheckBoxRadioButtonToggleButton 75

CheckBoxRadioButtonToggleButton的父類是誰,及它們都是什麼?

答案:這三者所有是Button的子類,都是與處理用戶點擊事件有關的控件。其中,CheckBox是選擇框,RadioButton是單選按鈕,而ToggleButton是雙狀態選擇按鈕。

Android經常使用控件的信息。

答案:單選框(RadioButtonRadioGroup)

RadioGroup用於對單選框進行分組,相同組內的單選框只有一個單選框被選中。

事件:setOnCheckedChangeListener(),處理單選框被選擇事件。把RadioGroup.OnCheckedChangeListener實例做爲參數傳入。

多選框(CheckBox):

每一個多選框都是獨立的,能夠經過迭代全部的多選框,而後根據其狀態是否被選中在獲取其值。

事件:setOnCheckChangeListener()處理多選框被選擇事件。把CompoundButton.OnCheckedChangeListener實例做爲參數傳入

下拉列表框(Spring)

Spinner.getItemAtPosition(Spinner.getSelectedItemPosition());獲取下拉列表框的值。

事件:setOnItemSelectedListener(),處理下拉列表框被選擇事件把AdapterView.OnItemSelectedListener實例做爲參數傳入;

拖動條(SeekBar)

SeekBar.getProgress()獲取拖動條當前值

事件:setOnSeekBarChangeListener(),處理拖動條值變化事件,把SeekBar.OnSeekBarChangeListener實例做爲參數傳入。

菜單(Menu):

重寫ActivityonCreatOptionMenu(Menu menu)方法,該方法用於建立選項菜單,咋用戶按下手機的"Menu"按鈕時就會顯示建立好的菜單,在onCreatOptionMenu(Menu Menu)方法內部能夠調用Menu.add()方法實現菜單的添加。

重寫ActivityonMenuItemSelected()方法,該方法用於處理菜單被選擇事件。

進度對話框(ProgressDialog)

建立並顯示一個進度對話框:ProgressDialog.show(ProgressDialogActivity.this,"請稍等""數據正在加載中...."true)

設置對話框的風格:setProgressStyle()

ProgressDialog.STYLE_SPINNER  旋轉進度條風格(爲默認風格)

ProgressDialog.STYLE_HORIZONTAL 橫向進度條風格

android:gravityandroid:layout_gravity的區別

答案:LinearLayout有兩個很是類似的屬性:android:gravityandroid:layout_gravity。他們的區別在 於:android:gravity用於設置View組件的對齊方式,而android:layout_gravity用於設置Container組件的 對齊方式。

舉個例子,咱們能夠經過設置android:gravity="center"來讓EditText中的文字在EditText組件中居中顯示;同 時咱們設置EditTextandroid:layout_gravity="right"來讓EditText組件在LinearLayout中居中 顯示。來實踐如下:

正如咱們所看到的,在EditText中,其中的文字已經居中顯示了,而EditText組件本身也對齊到了LinearLayout的右側。

[html] view plaincopyprint?

<LinearLayout   

    xmlns:android="http://schemas.android.com/apk/res/android"   

    android:orientation="vertical"   

    android:layout_width="fill_parent"   

    android:layout_height="fill_parent">   

    <EditText   

        android:layout_width="wrap_content"   

        android:gravity="center"   

        android:layout_height="wrap_content"   

        android:text="one"   

        android:layout_gravity="right"/>   

</LinearLayout>  


這兩個屬性也能夠用於 Framlayout Textview 
等等,表示的意思大同小異。

paddingmargin的區別。

答案:padding填充的意思,指的是view中的contentview邊緣的距離,相似文本中的indent
margin
表示的是view的左邊緣與parent view的左邊緣的距離
margin
通常用來描述控件間位置關係,而padding通常描述控件內容和控件的位置關係。

簡單,padding是站在父 view的角度描述問題,它規定它裏面的內容必須與這個父view邊界的距離。margin則是站在本身的角度描述問題,規定本身和其餘(上下左右)的 view之間的距離,若是同一級只有一個view,那麼它的效果基本上就和padding同樣了。例如個人XML layout代碼以下:

view plaincopy to clipboardprint?

[html] view plaincopyprint?

<?xml version="1.0" encoding="utf-8"?>    

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    

    android:orientation="vertical"    

    android:layout_width="fill_parent"    

    android:layout_height="fill_parent"    

    android:paddingLeft="10dip"    

    android:paddingRight="10dip"    

    android:paddingTop="10dip"    

    android:paddingBottom="10dip"    

    >    

<TextView      

    android:layout_width="wrap_content"     

    android:layout_height="wrap_content"     

    android:background="#FF0000"    

    android:text="@string/hello"    

    android:paddingLeft="50dip"    

    android:paddingRight="50dip"    

    android:paddingTop="50dip"    

    android:paddingBottom="50dip"    

    android:layout_marginBottom="10dip"    

    />    

    <TextView      

    android:layout_width="wrap_content"     

    android:layout_height="wrap_content"     

    android:background="#FF0000"    

    android:text="@string/hello"    

    android:paddingLeft="50dip"    

    android:paddingRight="50dip"    

    android:paddingTop="50dip"    

    android:paddingBottom="50dip"    

    android:layout_marginBottom="10dip"    

    />    

    <TextView      

    android:layout_width="wrap_content"     

    android:layout_height="wrap_content"     

    android:background="#FF0000"    

    android:text="@string/hello"    

    android:paddingLeft="50dip"    

    android:paddingRight="50dip"    

    android:paddingTop="50dip"    

    android:paddingBottom="50dip"    

    android:layout_marginBottom="10dip"    

    />    

    <TextView      

    android:layout_width="wrap_content"     

    android:layout_height="wrap_content"     

    android:background="#FF0000"    

    android:text="@string/hello"    

    android:paddingLeft="50dip"    

    android:paddingRight="50dip"    

    android:paddingTop="50dip"    

    android:paddingBottom="50dip"    

    android:layout_marginBottom="10dip"    

    />    

</LinearLayout>    

 

 那麼我會獲得以下的效果,圖上已經很明確的標出來區別咯。

註冊廣播接收者兩種方式的區別。

答案:如今咱們就來實現一個簡單的廣播程序。Android提供了兩種註冊廣播接受者的形式,分別是在程序中動態註冊和在xml中指定。他們之間的區別就是做用 的範圍不一樣,程序動態註冊的接收者只在程序運行過程當中有效,而在xml註冊的接收者無論你的程序有沒有啓動有會起做用。

相關文章
相關標籤/搜索