Android的視圖(View)組件


  Android的絕大部分UI組件都放在android.widget包及其子包、android,view包及其子包中,Android應用的所有UI組件都繼承了View類,View組件非常類似於Swing變成的JPanel,它代表一個空白的矩形區域。

  View類還有一個重要的子類:ViewGroup,但ViewGroup通常作為其他組件的容器使用。

  Android的所有UI組件都是建立在View、ViewGroup基礎之上的,Android采用了“組合器”設計模式來設計View和ViewGroup:ViewGroup是View的子類,因此ViewGroup也可被當成View使用,對於一個Android應用的圖形用戶界面來說,ViewGroup作為容器來生盛裝其他組件,而ViewGroup里除了可以包含普通View組件之外,還可以再次包含ViewGroup組件。

  Android提供了兩種方式來控制組件的行為:

    1.在XML布局文件中通過XML屬性進行控制

    2.在Java或Kotlin代碼中通過調用方法進行控制。

  實際上不管使用那種方式,他們控制Android用戶界面行為的本質是完全一樣的。大部分時候,控制UI組件的XML屬性還有對應的方法。

  對於View類而言,它是所有UI組件的基類,因此它包含的XML屬性和方法是所有組件都可以使用的。

XML屬性 相關方法 說明
android:alpha setAlpha(float) 設置該組件的透明度
android:background setBackgroundResource(int) 設置該組件的背景顏色
android:backgroundTint setBackgroundTintList(ColorStateList)

設置對背景顏色重新着色,該屬性要

與android:background結合使用

android:backgroundTintMode setBackgroundTintMode(PorterDuff.Mode)

設置對背景顏色着色的模式,該屬性

支持PorterDuff.Mode的各枚舉值

android:clickable setClickable(boolean) 設置該組件是否可以激發單擊事件
android:contentDescription setContentDescription(CharSequence) 設置該組件的內容描述信息
android:contextClickable setContextClickable(boolean)

設置該屬性是否可以激發context單

擊事件

android:drawingCacheQuality setDrawingCacheQuality(int) 設置該組件所使用的繪制緩存的質量
android:elevation setElevation(float)

設置該組件“浮”起來的高度,通過

設置該屬性可讓該組件呈現3D效果

android:fadeScrollbars setScrollbarFadingEnabled(boolean)

設置當不使用該組件的滾動條時,是否

淡出顯示滾動條 

android:fadingEdgeLength getVerticalFadingEdgeLength() 設置淡出邊界的長度
android:filterTouchesWhenObscured setFilterTouchesWhenObscured(boolean)

設置當該組件所在的窗口被其他窗口

遮擋時,是否過濾觸摸事件

android:fitsSystemWindows setFitsSystemWindows(boolean)

設置是否基於系統窗口(如狀態欄)

來調整視圖布局 

android:focusable setFocusable(boolean) 設置該組件是否可以得到焦點
android:focueableInTouchMode setFocusableInTouchMode(boolean)

設置該組件在觸摸模式下是否可以得到

焦點

android:foreground setForeground(Drawable) 設置繪制到組件內容上面的Drawable 
android:foregroundGravity setForegroundGravity(int) 設置繪制前景Drawable時的對齊方式
android:foregroundTint setForegroundTintList(ColorStateList) 設置對前景Drawable重新着色
android:foregroundTintMode setForegroundTintMode(PorterDuff.Mode) 設置對前景Drawable着色的模式
android:hapticFeedbackEnabled setHapticFeedbackEnabled(boolean)

設置該組件是否能對諸如長按這樣的事件

啟用觸覺反饋

android:id setId(int)

設置該組件的唯一標識。在Java代碼中

可通過findViewById來獲取它 

android:isScrollContainer setScrollContainer(boolean) 設置該組件是否作為可滾動容器使用 
android:keepScreenOn setKeepScreenOn(boolean) 設置該組件是否會強制手機屏幕一直打開 
android:layerType setLayerType(int,Paint)  設置該組件使用的圖層類型 
android:layoutDirection setLayoutDirection(int) 

設置該組件的布局方式。該屬性支持

ltr(從左到右)、rtl(從右到左)、inherit

(與父容器相同)和locale四種值 

android:longClickable setLongClickable(boolean)  設置該組件是否可以響應長單擊事件 
android:minHeight setMinimumHeight(int)  設置該組件的最小高度 
android:minWidth setMinimumWidth(int)  設置該組件的最小寬度 
android:nextFocusDown setNextFocusDownId(int) 

設置焦點在該組件上,且單擊向下鍵時獲得

焦點的組件ID 

android:nextFocusForward setNextFocusForwardId(int) 

設置焦點在該組件上,且單擊向前鍵時獲得

焦點的組件ID 

android:nextFocusLeft setNextFocusLeftId(int)

設置焦點在該組件上,且單擊向左鍵時獲得

焦點的組件ID 

android:nextFocusRight setNextFocusRightId(int)

設置焦點在該組件上,且單擊向右鍵時獲得

焦點的組件ID 

android:nextFocusUp setNextFocusUpId(int)

設置焦點在該組件上,且單擊向上鍵時獲得

焦點的組件ID 

android:onClick  

為該組件的單擊事件綁定監聽器

android:padding setPadding(int,int,int,int) 在組件的四邊設置填充區域
android:paddingBottom setPadding(int,int,int,int) 在組件的下邊設置填充區域
android:paddingEnd setPaddingRelative(int,int,int,int)  相對布局時,在組件結尾處設置填充區域 
android:paddingHorizontal setPaddingRelative(int,int,int,int)  在組件的左、右兩邊設置填充區域 
android:paddingLeft setPadding(int,int,int,int) 在組件的左邊設置填充區域 
android:paddingRight setPadding(int,int,int,int)  在組件的右邊設置填充區域 
android:paddingStart setPaddingRelative(int,int,int,int)  相對布局時,在組件起始處設置填充區域 
android:paddingTop setPadding(int,int,int,int)  在組件的上邊設置填充區域 
android:paddingVertical setPadding(int,int,int,int)  在組件的上、下兩邊設置填充區域 
android:rotation setRotation(float) 設置該組件的旋轉的角度 
android:rotationX setRotationX(float)  設置該組件繞X軸旋轉的角度 
android:rotationY setRotationY(float)  設置該組件繞Y軸旋轉的角度 
android:saveEnabled setSaveEnabled(boolean)

如果設置為false,那么當該組件被凍結時

不會保存它的狀態 

android:scaleX setScaleX(float) 設置該組件在水平方向的縮放比
android:scaleY setScaleY(float)  設置該組件在垂直方向的縮放比 
android:scrollIndicators setScrollIndicators(int) 

設置組件滾動時顯示哪些滾動條,默認值

是“top|bottom”,即上、下顯示 

android:scrollX   該組件初始化后的水平滾動偏移 
android:scrollY   該組件初始化后的垂直滾動偏移 
android:scrollbarAlwaysDrawHorizontalTrack  

設置該組件是否總是顯示水平滾動條的

軌道 

android:scrollbarAlwaysDrawVerticalTrack  

設置該組件是否總是顯示垂直滾動條的

軌道 

android:scrollbarDefaultDelayBeforeFade setScrollBarDefaultDelayBeforeFade(int)  設置滾動條在淡出隱藏之前延遲多少毫秒
android:scrollbarSize setScrollBarSize(int) 

設置垂直滾動條的寬度和水平滾動條的高度 

android:scrollbarStyle setScrollBarStyle(int) 

設置滾動條的風格和位置。該屬性支持如下

屬性值:

  1.insideOverlay

  2.insideInset

  3.outsideOverlay

  4.outsideInset 

android:scrollbarThumbHorizontal  

設置該組件的水平滾動條的滑塊對應的

Drawable對象

android:scrollbarThumbVertical  

設置該組件的垂直滾動條的滑塊對應的

Drawable對象

android:scrollbarTrackHorizontal  

設置該組件的水平滾動條的軌道對應的

Drawable對象

android:scrollbarTrackVertical  

設置該組件的垂直滾動條的軌道對應的

Drawable對象

android:scrollbars  

定義該組件滾動時顯示幾個滾動條。

該屬性支持如下屬性值:

  1.none:不顯示滾動條

  2.horizontal:顯示水平滾動條

  3.vertical:顯示垂直滾動條

android:soundEffectsEnabled setSoundEffectsEnabled(boolean)  設置該組件被單擊時是否使用音效
android:tag  

為該組件設置一個字符串類型的tag值。

接下來可通過View的getTag()獲取該

字符串,或通過findViewWithTag()

查找該組件 

android:textAlignment setTextAlignment(int)  設置組件內文字的對齊方式
android:textDirection setTextDirection(int)  設置組件內文字的排列方式 
android:theme   設置該組件的主題 
android:transformPivotX setPivotX(float)  設置該組件旋轉時旋轉中心的X坐標 
android:transformPivotY setPivotY(float)  設置該組件旋轉時旋轉中心的Y坐標 
android:transitionName  

為該View指定名字以便Transition能夠

識別它

android:translationX setTranslationX(float) 設置該組件在X方向上的位移 
android:translationY setTranslationY(float)  設置該組件在Y方向上的位移
android:translationZ setTranslationZ(float)  設置該組件在Z方向(垂直屏幕方向)上的位移 
android:visibility setVisibility(int) 設置該組件是否可見 

**Drawable是Android提供的一個抽象基類,它代表了“可以被繪制出來的某種東西”,Drawable包括了大量子類,比如ColorDrawable代表顏色Drawable,ShapeDrawable代表幾何形狀Drawable。各種Drawable可用於定制UI組件的背景等外觀。

**上面表格中介紹View組件時列出的setBackgroundTintList、setElevation(float)、setTranslationZ(float)方法都是Material Design中的功能,他們的作用也基本相似。它們都可用於設置該組件垂直屏幕“浮”起來的效果,通過該屬性可讓該組件呈現3D效果。當該組件垂直於屏幕“浮”起來之后,組件下的實時陰影就會自動呈現。

  ViewGroup繼承了View類,當然也可以當成普通View來使用,但ViewGroup主要還是當成容器類使用。但由於ViewGroup是一個抽象類,因此在實際使用中通常總是使用ViewGroup的子類來作為容器,例如各種布局管理器。

  ViewGroup容器控制其子組件的分布依賴於ViewGroup.LayoutParams、ViewGroup.MarginLayoutParams兩個內部類。這兩個內部類中都提供了一些XML屬性,ViewGroup容器中的子組件可以指定這些XML屬性

XML屬性 說明
android:layout_height 指定該子組件的布局高度
android:layout_width 指定該子組件的布局寬度

  android:layout_height和android:layout_width兩個屬性支持如下兩個屬性值

    1.match_parent(早期叫fill_parent):指定子組件的高度、寬度與父容器組件的高度、寬度相同(實際上還要減去填充的空白距離)

    2.wrap_content:指定子組件的大小恰好能包裹它的內容即可。

**Android的布局機制中,Android組件的大小不僅受它實際的寬度、高度控制,還受它的布局高度與布局寬度控制。例如一個組件的寬度為30pt,如果將它的布局寬度設置為match_parent,那么該組件的寬度將會被“拉寬”到占滿它所在的父容器;如果將它的布局寬度設置為wrap_content,那么該組件的寬度才會是30pt。

  ViewGroup.MarginLayoutParams用於控制子組件周圍的頁邊距(Margin,也就是組件四周的留白),它支持的XML屬性及相關方法如下表所示:

XML屬性 相關方法 說明
android:layout_margin setMargins(int,int,int,int) 指定該子組件四周的頁邊距
android:layout_marginBottom setMargins(int,int,int,int) 指定該子組件下邊的頁邊距
android:layout_marginEnd setMargins(int,int,int,int) 指定該子組件結尾處的頁邊距
android:layout_marginHorizontal setMargins(int,int,int,int) 指定該子組件左、右兩邊的頁邊距
android:layout_marginLeft setMargins(int,int,int,int) 指定該子組件左邊的頁邊距
android:layout_marginRight setMargins(int,int,int,int) 指定該子組件右邊的頁邊距
android:layout_marginStart setMargins(int,int,int,int) 指定該子組件起始處的頁邊距
android:layout_marginTop setMargins(int,int,int,int) 指定該子組件上邊的頁邊距
android:layout_marginVertical setMargins(int,int,int,int) 指定該子組件上、下兩邊的頁邊距


免責聲明!

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



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