当前位置: 首页 > news >正文

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调用来实现有边框的调用

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

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

1.2 EditText控件

常用属性
属性含义
hint默认提示文本
textColor设置文本颜色(#000000~#FFFFFF)
height设置控件高度
width设置控件宽度
textStyle设置字体样式(粗体、斜体)
selectAllOnFocus获取焦点后全选组件内所有文本内容
限制EditText输入类型
属性含义
inputTypephone(拨号键盘)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控件(多选按钮)

示例说明
<RadioGroupandroid:id="@+id/radioGroup"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><RadioButtonandroid:id="@+id/btnMan"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""android:checked="true"/><RadioButtonandroid:id="@+id/btnWoman"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""/>
</RadioGroup><CheckBoxandroid:id="@+id/cb_one"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="香蕉"/>
<CheckBoxandroid:id="@+id/cb_two"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="苹果"/>
<CheckBoxandroid: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);
示例说明
<ToggleButtonandroid:id="@+id/tbtn_open"android:layout_width="wrap_content"android:layout_height="wrap_content"android:checked="true"android:textOff="关闭声音"android:textOn="打开声音" /><Switchandroid: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设置运行被拉伸的列的列序号

示例说明

隐藏列

<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>

拉伸列

<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>  

收缩列

<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设置前景图像显示的位置
// 简单的帧布局使用
<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横向跨行数

相关文章:

  • 【新能源大巴BMS结构与乘用车的区别】
  • 每日一题——Python实现PAT甲级1041 Be Unique(举一反三+思想解读+逐步优化)
  • java使用资源过高排查
  • 解析Java中1000个常用类:Cloneable类,你学会了吗?
  • linux-gpio
  • 【代码随想录算法训练营第37期 day21 | LeetCode530.二叉搜索树的最小绝对差、501.二叉搜索树中的众数、236. 二叉树的最近公共祖先】
  • Java集合【超详细】
  • 实战经验分享之移动云快速部署Stable Diffusion SDXL 1.0
  • K8S中Prometheus+Grafana监控
  • Wpf 使用 Prism 实战开发Day30
  • YOLOv5训练自定义数据集模型的参数与指令说明
  • Flutter 中的 SliverFillRemaining 小部件:全面指南
  • Golang | Leetcode Golang题解之第120题三角形最小路径和
  • kafka-消费者组-发布订阅测试
  • linux同步搭建多台服务器
  • 【Leetcode】101. 对称二叉树
  • Android路由框架AnnoRouter:使用Java接口来定义路由跳转
  • - C#编程大幅提高OUTLOOK的邮件搜索能力!
  • CSS实用技巧
  • Cumulo 的 ClojureScript 模块已经成型
  • JavaScript 奇技淫巧
  • Markdown 语法简单说明
  • PHP 的 SAPI 是个什么东西
  • Spark VS Hadoop:两大大数据分析系统深度解读
  • 彻底搞懂浏览器Event-loop
  • 计算机在识别图像时“看到”了什么?
  • 浏览器缓存机制分析
  • 前端之React实战:创建跨平台的项目架构
  • 树莓派 - 使用须知
  • 一个完整Java Web项目背后的密码
  • 转载:[译] 内容加速黑科技趣谈
  • 06-01 点餐小程序前台界面搭建
  • shell使用lftp连接ftp和sftp,并可以指定私钥
  • 阿里云ACE认证之理解CDN技术
  • ​业务双活的数据切换思路设计(下)
  • #QT项目实战(天气预报)
  • #大学#套接字
  • (c语言+数据结构链表)项目:贪吃蛇
  • (el-Transfer)操作(不使用 ts):Element-plus 中 Select 组件动态设置 options 值需求的解决过程
  • (pojstep1.1.2)2654(直叙式模拟)
  • (六)vue-router+UI组件库
  • (四)事件系统
  • (五)c52学习之旅-静态数码管
  • (一)WLAN定义和基本架构转
  • (原)记一次CentOS7 磁盘空间大小异常的解决过程
  • (转) RFS+AutoItLibrary测试web对话框
  • (转)程序员技术练级攻略
  • (转载)Google Chrome调试JS
  • ... fatal error LINK1120:1个无法解析的外部命令 的解决办法
  • ./include/caffe/util/cudnn.hpp: In function ‘const char* cudnnGetErrorString(cudnnStatus_t)’: ./incl
  • .apk文件,IIS不支持下载解决
  • .env.development、.env.production、.env.staging
  • .NET C# 操作Neo4j图数据库
  • .NET和.COM和.CN域名区别
  • .NET中统一的存储过程调用方法(收藏)