android應用的界面編程----View與ViewGroup的概念


1 UI OverView

  Android中所有的UI元素都是通過View與ViewGroup來構建的,View是指屏幕中一塊可與用戶進行交互的空白,類似於java界面編程中的JPanel。為了界面布局,A Group對象可以包含多個View或ViewGroup(由於ViewGroup是View的子類)。

  View的子類集合主要包括各種Input 控件,ViewGroup的子類集合包括各種布局控件。

2 user Interface layout

  每個應用程序的界面UI控件都是由如下圖中的層次關系構成。每個ViewGroup都是成員View(input 控件或者UI上定義的小部件)的不可見容器。層次結構根據需要可復雜可簡單(越簡單性能越好)。

  用戶界面構建有兩種方法:java代碼構建(跟java界面編程相似);借助xml進行界面布局。對於View類而言,它是所有UI控件的基類,所以View所包含的xml屬性和方法是所有組件都可以使用的。如下是一個layout文件示例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent"
              android:orientation="vertical" >
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="I am a TextView" />
    <Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="I am a Button" />
</LinearLayout>
View Code

  當你的App加載上述的布局資源的時候,Android會將布局中的每個節點進行實例化成一個個對象,然后你可以為這些定義一些額外的行為,查詢對象的狀態,或者修改布局。 

3 view類的xml屬性及其說明

XML屬性 說明
android:alpha 設置該組件的透明度
android:background 設置該組件的背景顏色
android:clickable 設置該組件是否可以激發單擊事件
android:contentDescription 設置該組件的主要描述信息
android:drawingCasheQuanlity 設置該組件所使用的繪制緩存質量
android:fadeScrollbars 設置該組件不使用滾動條時,是否淡出顯示滾動條
android:fadingEdge 設置滾動該組件時邊界是否使用淡出效果
android:fadingEdgeLength 設置淡出邊界長度
android:focusable 設置該組件是否可以得到焦點
android:focusableInTouchMode 設置該組件在觸摸模式下是否可以得到焦點
android:id 設置該組件ViewById來獲取它
android:isScrollContainer 設置該組件是否可以作為可滾動容器使用
android:keepScreenOn 設置該組件是否會強制手機屏幕一直打開
android:longClickable 設置該組件是否可以響應長單擊事件
android:minHeight 設置該組件的最小高度
android:minWidth 設置該組件的最小寬度
android:nextFocusDown 設置焦點在該組件上,且按向下鍵時獲得焦點的組件id
android:nextFocusRight 設置焦點在該組件上,且按向右鍵時獲得焦點的組件id
android:nextFocusLeft 設置焦點在該組件上,且按向左鍵時獲得焦點的組件id
android:nextFocusUp 設置焦點在該組件上,且按向上鍵時獲得焦點的組件id
android:onClick 為該組件的單擊事件綁定監聽器
android:padding 在組件的四邊設置填充區域
android:paddingLeft 在組件的左邊設置填充區域
android:paddingRight 在組件的右邊設置填充區域
android:paddingBottom 在組件的底邊設置填充區域
android:paddingTop 在組件的頂邊設置填充區域
android:rotation 設置該組件的旋轉角度
android:rotationX 設置該組件繞X軸旋轉的角度
android:rotationY 設置該組件繞Y軸旋轉的角度
android:saveEnabled 設置為false,當該組件被凍結時不會保存其狀態
android:scaleX 設置該組件水平方向的縮放比
android:scaleY 設置該組件垂直方向的縮放比
android:scrollX 設置該組件初始化后的水平滾動條偏移
android:scrollY 設置該組件初始化后垂直滾動條偏移

android:scrollbarAlwaysDrawHorizontalTrack

設置該組件是否顯示水平滾動條的軌跡
android:scrollbarAlwaysDrawVerticalTrack 設置該組件是否顯示垂直滾動條的軌跡
android:scrollbarDefaultDelayBeforeFade 設置滾動條在淡出隱藏之前延遲多少毫秒
android:scrollbarFadeDuration 設置滾動條淡出隱藏需要多少秒
android:scrollbarSize 設置垂直滾動條的寬度和水平滾動條的高度
android:scrollbarStyle 設置滾動條的風格和位置:insideOverlay
android:scrollThumbHorizontal 設置該組件水平滾動滑塊對對應的Drawable對象
android:scrollThumbVertical 設置該組件垂直滾動滑塊對對應的Drawable對象
android:scrollTrackbHorizontal 設置該組件水平滾動軌道對對應的Drawable對象
android:scrollTrackbVertical 設置該組件垂直滾動軌道對對應的Drawable對象
android:scrollbar 設置該組件滾動時顯示幾個滾動條:none horizontal,vertical
android:soundEffectsEnabled 設置該組件被單擊時是否使用音效
android:tag 為組件設置一個字符串tag值,可以通過view的getTag 獲取字符串,或通過findViewByTag查找該組件

android:transformPivotX

設置該組件旋轉時旋轉中心的X坐標
android:transformPivotY 設置該組件旋轉時旋轉中心的Y坐標
android:translationX 設置該組件在x方向上的位移
android:translationY 設置該組件在Y方向上的位移
android:visibility 設置該組件是否可見

 4 ViewGroup類

  ViewGroup繼承了View類,故其可以當作普通的View來使用;但ViewGroup是一個抽象類,實際都是使用ViewGroup的子類作為容器類。ViewGroup容器控制容器內組件的分布依賴於ViewGroup.LayoutParams與ViewGroup.MarginLayoutParams兩個內部類。這兩個內部類也提供了一些XML屬性,ViewGroup容器內子組件可以指定這些xml屬性。

4.1 ViewGroup.LayoutParams所支持的兩個xml屬性

xml屬性 說明
android:layout_height 指定該子組件的布局高度fill_parent/match_parent/wrap_parent
android:layout_width 指定該子組件的布局寬度fill_parent/match_parent/wrap_parent

4.2 ViewGroup.MarginLayoutParams:控制子組件周圍的頁邊距

xml屬性 說明
android:layout_marginBottom 指定該子組件下邊的頁邊距
android:layout_marginLeft 指定該子組件左邊的頁邊距
android:layout_marginRight 指定該子組件右邊的頁邊距
android:layout_marginTop 指定該子組件上邊的頁邊距

 

 

 

 

 

3 線性布局管理器LinearLayout

  線性布局由LinearLayout類代表,將容器里的組件一個挨一個地排列起來,LinearLayout不會自動換行,到末尾后剩余的組件將不會被顯示出來。LinearLayout支持的常用xml屬性及其說明。

xml屬性 說明
android:baselineAligned 該屬性為false,將會阻止布局管理器與它的子元素基線對其
android:divider 設置垂直布局時,兩個按鈕之間的分隔條
android:gravity 設置布局管理器內組件的對齊方式。該屬性支持top/button/left/right/center_vertical/fill_vertical...
android:measureWithLargestChild 當屬性設置為true時,所有帶權重的子元素都會具有最大元素的最小尺寸
android:orientation 設置布局管理器內組件的排列方式vertical/horizontal默認值

3.1 LinearLayout.LayoutParams控制

xml屬性 說明
android:layout_gravity 指定該布局管理器內子組件布局方式
android:layout_weight 指定該子元素在linearLayout中所占的權重


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM