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) 指定该子组件上、下两边的页边距
原文地址:https://www.cnblogs.com/fanqisoft/p/11006382.html