Android UI控件详细解析(四)

1.UI控件

1.1 TextView控件

常用属性
属性 含义
id 给当前控件定义了一个唯 一标识符
layout_width 高度,单位:dp (wrap_content, match_parent)
layout_height 宽度,单位:dp (wrap_content, match_parent)
background 设置背景图片
text 指定控件中显示的文本内容
gravity 来指定文字的对齐方式(top 、bottom 、left 、right 、center)
textSize 指定控件中文字的大小
textColor 指定控件中文字的颜色(#000000~#FFFFFF)
textStyle 指定控件中文字的样式
带阴影的TextView)(Drawable资源文件使用)
属性 含义
shadowColor 设置阴影颜色,需要与shadowRadius一起使用
shadowRadius 设置阴影的模糊程度,设为0.1就变成字体颜色(建议为3.0)
shadowDx 设置阴影在水平方向的偏移,就是水平方向阴影开始的横坐标位置
shadowDy 设置阴影在水平方向的偏移,就是水平方向阴影开始的横坐标位置
带边框的TextView(Drawable资源文件使用)

通过设置边框的Drawable文件,然后通过TextView的background调用来实现有边框的调用

xml 复制代码
<!--示例说明-->
<TextView
          android:background="@drawable/txt_rectborder"
          />
带图片的TextView(Drawable资源文件使用)

设置图片的核心其实就是Drawable资源文件,它可以设置四个方向的图片: drawableTop(上)、drawableButtom(下)、drawableLeft(左)、drawableRight(右) 另外,你也可以使用drawablePadding来设置图片与文字间的间距!

1.2 EditText控件

常用属性
属性 含义
hint 默认提示文本
textColor 设置文本颜色(#000000~#FFFFFF)
height 设置控件高度
width 设置控件宽度
textStyle 设置字体样式(粗体、斜体)
selectAllOnFocus 获取焦点后全选组件内所有文本内容
限制EditText输入类型
属性 含义
inputType phone(拨号键盘)date(日期键盘)time(时间键盘)
设置最小行、最多行、单行、多行、自动换行
属性 含义
singleLine 设置只允许单行输入,而且不会滚动
minLines 设置最小行数
maxLines 设置最大行数
textScaleX 设置字与字的水平间隔
textScaleY 设置字与字的垂直间隔
capitalize 设置英文字母大写(sentences【第一个字母大写】、word【每一个单词首字母大写,以空格区分单词】、characters【每一个英文字母都大写】)
设置内边距
属性 含义
marginTop 上边距
marginBottom 下边距
marginRight 右边距
marginLeft 左边距

1.3 Button按钮与ImageView图像按钮

常用属性
属性 描述
drawable 放一个drawable资源
drawableTop 可拉伸要绘制的文本的上面
drawableBottom 可拉伸要绘制的文本的下面
drawableLeft 可拉伸要绘制的文本的左面
drawableRight 可拉伸要绘制的文本的右面
text 设置显示文本
textColor 设置显示文本颜色
textSize 设置显示文本大小
background 可拉伸使用的背景
onclick 设置点击事件(不常用)
Button的状态属性
属性 描述
stated_pressed 是否按下,如一个按钮触摸或者点击
state_focused 是否取得焦点,比如用户选择了一个文本框
state_hovered 光标是否悬停,通常与focused state相同
state_selected 被选中状态
state_checkable 组件是否能被check。如:RadioButton是可以被check的
state_checked 被checked了,如:一个RadioButton可以被check了的
state_enabled 能够接受触摸或者点击事件
state_activated 被激活
state_window_focused 应用程序是否在前台,当有通知栏被拉下来或者一个对话框弹出的时候应用程序就不在前台了

1.4 ImageView控件的src属性和background属性的区别

  1. background通常指的都是背景 ,而src指的是内容!!
  2. 当使用src填入图片时,是按照图片大小直接填充 ,并不会进行拉伸;使用background填入图片,则是会根据ImageView给的值进行拉伸。

1.5 RadioButton控件(单选按钮)和CheckBox控件(多选按钮)

示例说明
xml 复制代码
<RadioGroup
    android:id="@+id/radioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/btnMan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="男"
        android:checked="true"/>

    <RadioButton
        android:id="@+id/btnWoman"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="女"/>
</RadioGroup>

<CheckBox
        android:id="@+id/cb_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="香蕉"/>
<CheckBox
        android:id="@+id/cb_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="苹果"/>
<CheckBox
        android:id="@+id/cb_three"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="菠萝"/>

1.6 ToggleButton控件(开关按钮)Switch控件(开关)

ToggleButton常用属性
属性 描述
disabledAlpha 设置按钮在禁用时的透明度
textOff 按钮没有被选中时显示的文字
textOn 按钮被选中时显示的文字
Switch常用属性
属性 描述
showText 设置on/off的时候是否显示文字(true / false)
splitTrack 是否设置一个间隙,让滑块与底部图片分隔(true / false)
switchMinWidth 设置开关的最小宽度
switchPadding 设置滑块内文字的间隔
switchTextAppearance 设置开关的文字外观(用得少)
textOff 按钮没有被选中时显示的文字
textOn 按钮被选中时显示的文字
textStyle 文字风格,粗体,斜体写划线那些
track 底部的图片
thumb 滑块的图片
typeface 设置字体(sans, serif, monospace) 自定义字体文件: 1. 将字体文件保存在assets/fonts/目录下 2. 在Java代码中设置: Typeface typeFace =Typeface.createFromAsset(getAssets(),"fonts/HandmadeTypewriter.ttf"); textView.setTypeface(typeFace);
示例说明
xml 复制代码
<ToggleButton
    android:id="@+id/tbtn_open"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:textOff="关闭声音"
    android:textOn="打开声音" />

<Switch
    android:id="@+id/swh_status"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOff=""
    android:textOn=""
    android:thumb="@drawable/thumb_selctor"
    android:track="@drawable/track_selctor" />

1.7 ProgressBar控件

基本用法
属性 描述
max 进度条最大值
style 设置进度条的样式:环形或者水平进度条(Horizontal)
progress 进度条已完成进度值
progressDrawable 设置轨道对应的Drawable对象
indeterminate 如果设置成true,则进度条不精确显示进度
indeterminateDrawable 设置不显示进度的进度条的Drawable对象
indeterminateDuration 设置不精确显示进度的持续时间
secondaryProgress 二级进度条,类似于视频播放的一条是当前播放进度,一条是缓冲进度,前者通过progress属性进行设置

1.8 SeekBar控件

基本用法
属性 描述
max 滑动条的最大值
progress 滑动条的当前值
secondaryProgress 二级滑动条的进度
thumb 滑块的drawable

1.9 RatingBar控件

基本用法
属性 描述
isIndicator 是否用作指示,用户无法更改,默认false
numStars 显示多少个星星,必须为整数
rating 默认评分值,必须为浮点数
stepSize 评分每次增加的值,必须为浮点数
style 样式选择

2. 布局

Android中有六大布局,分别是:LinearLayout(线性布局),RelativeLayout(相对布局),TableLayout(表格布局) FrameLayout(帧布局),AbsoluteLayout(绝对布局),GridLayout(网格布局)

2.1 线性布局管理(LinearLayout)

属性 含义
layout_height 高度,单位:dp (wrap_content, match_parent)
layout_weight 宽度,单位:dp (wrap_content, match_parent)
orietation 方向(vertical,horizontal)
gravity 对齐方式(left, right, center, top, bottom...)
background 背景(颜色[color]、图片[drawable]、选择器[selector])
padding 内边距 (paddingLeft, paddingRight, paddingTop, paddingBottom)
margin 外边距 (marginLeft, marginRight, marginTop, marginBottom)
layout_gravity 本元素相对于父元素的重力方向
orientation 本元素所有子元素的重力方向

权重

属性 含义
weight 比重

分割线

属性 含义
divider 为LinearLayout设置分割线图片
showDividers 设置分割线所在位置
dividerPadding 设置分割线的Padding

2.2 相对布局管理(RelativeLayout)

基本属性

属性 描述
gravity 设置让其内组件的对齐方式
ignoreGravity 设置了该属性为true的属性的组件,将不受gravity属性的影响

根据父容器定位

属性 描述
layout_alignParentLeft 左对齐
layout_alignParentRight 右对齐
layout_alignParentTop 顶部对齐
layout_alignParentBottom 底部对齐
layout_centerHorizontal 水平居中
layout_centerVertical 垂直居中
layout_centerInParent 中间位置

根据兄弟组件定位

属性 描述
layout_toLeftOf 参考组件的左边
layout_toRightOf 参考组件的右边
layout_above 参考组件的上方
layout_below 参考组件的下方
layout_alignTop 对其参考组件的上边界
layout_alignBottom 对其参考组件的下边界
layout_alignLeft 对其参考组件的左边界
layout_alignRight 对其参考组件的右边界

偏移量(设置组件与父容器的边距)

属性 描述
layout_margin 设置组件爱你上下左右的偏移量
layout_marginLeft 设置组件离左边的偏移量
layout_marginRight 设置组件离右边的偏移量
layout_marginTop 设置组件离上面的偏移量
layout_marginBottom 设置组件离下面的偏移量

填充(设置组件中内部元素间的边距)

属性 描述
padding 往内部元素的上下左右填充一定的边距
paddingLeft 往内部元素的左边填充一定的边距
paddingRight 往内部元素的右边填充一定的边距
paddingTop 往内部元素的上面填充一定的边距
paddingBottom 往内部元素的下面填充一定的边距

2.3 表格布局管理(TableLayout)

常用属性

属性 描述
collapseColumns 设置需要被隐藏的列的序列号
shrinkColumns 设置允许被收缩的列的列序号
stretchColumns 设置运行被拉伸的列的列序号

示例说明

隐藏列

xml 复制代码
<TableLayout  
    android:id="@+id/TableLayout2"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:collapseColumns="0,2" >  

    <TableRow>  

        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="one" />  

        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="two" />  

        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="three" />  

        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="four" />  

        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="five" />  
    </TableRow>  
</TableLayout>

拉伸列

xml 复制代码
<TableLayout    
    android:id="@+id/TableLayout2"    
    android:layout_width="fill_parent"    
    android:layout_height="wrap_content"    
    android:stretchColumns="1" >    
    
    <TableRow>    
    
        <Button    
            android:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:text="one" />    
    
        <Button    
            android:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:text="two" />    
    
        <Button    
            android:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:text="three" />    
    
        <Button    
            android:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:text="four" />                 
    </TableRow>    
</TableLayout>  

收缩列

xml 复制代码
<TableLayout  
    android:id="@+id/TableLayout2"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:shrinkColumns="1" >  

    <TableRow>  

        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="one" />  

        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="two" />  

        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="three" />  

        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="four" />  

        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="five" />  

        <TextView  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="文本XX" />  
    </TableRow>  
</TableLayout>

2.4 帧布局管理(FrameLayout)

常用属性

属性 描述
foreground 设置改帧布局容器的前景图像
foregroundGravity 设置前景图像显示的位置
xml 复制代码
// 简单的帧布局使用
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    xmlns:tools="http://schemas.android.com/tools"    
    android:id="@+id/FrameLayout1"    
    android:layout_width="match_parent"    
    android:layout_height="match_parent"    
    tools:context=".MainActivity"     
    android:foreground="@drawable/logo"    
    android:foregroundGravity="right|bottom">    
    
    <TextView    
        android:layout_width="200dp"    
        android:layout_height="200dp"    
        android:background="#FF6143" />    
    <TextView    
        android:layout_width="150dp"    
        android:layout_height="150dp"    
        android:background="#7BFE00" />    
     <TextView    
        android:layout_width="100dp"    
        android:layout_height="100dp"    
        android:background="#FFFF00" />    
        
</FrameLayout>    

2.5 网格布局管理(GridLayout)

设置排列对齐

属性 描述
orientation 设置排列方式
layout_gravity 设置对其方式

设置几行几列

属性 描述
rowCount 设置行数
columnCount 设置列数

设置组件所在的行列

属性 描述
layout_row 第几行,0起
layout_column 第几列,0起

设置组件横跨几行几列

属性 描述
rowSpan 纵向跨行数
column 横向跨行数
相关推荐
火红的小辣椒19 分钟前
XSS基础
android·web安全
勿问东西2 小时前
【Android】设备操作
android
五味香2 小时前
C++学习,信号处理
android·c语言·开发语言·c++·学习·算法·信号处理
图王大胜4 小时前
Android Framework AMS(01)AMS启动及相关初始化1-4
android·framework·ams·systemserver
工程师老罗6 小时前
Android Button “No speakable text present” 问题解决
android
小雨cc5566ru7 小时前
hbuilderx+uniapp+Android健身房管理系统 微信小程序z488g
android·微信小程序·uni-app
小雨cc5566ru8 小时前
微信小程序hbuilderx+uniapp+Android 新农村综合风貌旅游展示平台
android·微信小程序·uni-app
小雨cc5566ru8 小时前
小程序 uniapp+Android+hbuilderx体育场地预约管理系统的设计与实现
android·小程序·uni-app
佛系小嘟嘟10 小时前
Android-由switch-case和view.getId()引起的bug:错误:需要常量表达式 的解决办法
android·bug
勿问东西10 小时前
【Android】事件
android