持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第4天,点击查看活动详情
上一篇中我们了解到了 TextView 基本的使用方法和属性。我们都知道在学习编程的过程中,最重要的就是去动手写,不然到真正使用的时候还是一脸茫然。接下来叫我们来写几个在开发过程中经常使用到的例子。结合例子是我们更好的理解TextView。
TextView 文本超出省略号
首先写一个TextView
-> 文本内容为 “我是内容,我是内容。。。”
-> 宽度随父容器,高度自适应
-> 让文本单行显示,singleLine
-> 定义省略号位置,ellipsize; start 起始位置 end 结束位置 middle 居中显示 none marquee 跑马灯效果
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是内容,我是内容,我是内容,我是内容,我是内容,我是内容,我是内容,我是内容,我是内容,我是内容,我是内容,我是内容,"
android:singleLine="true"
android:ellipsize="start | middle | end"
/>
TextView 文字跑马灯
-> 把ellipsize 设置为marquee
-> 设置可以通过触摸获取焦点, focusableInTouchMode
这个时候按照文档来说文本就会跑动起来,但事实却并非如此。经过一番探寻,最后找到一个解决方法, 在TextView 标签中加一个 < requestFocus />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是内容,我是内容,我是内容,我是内容,我是内容,我是内容,我是内容,我是内容,我是内容,我是内容,我是内容,我是内容,"
android:singleLine="true"
android:ellipsize="marquee"
android:focusableInTouchMode="true"
>
<requestFocus/>
</TextView>
TextView 文本带有阴影效果
阴影有四个属性要搭配一起用 -> 阴影颜色,shadowColor -> 阴影的模糊程度, shadowRadius -> 阴影的水平方向的偏移值,shawowDx -> 阴影的竖直方向的偏移值,shawowDy
TextView 插入图片
在文本的左侧插入一个图片是,并且使得图片与文本直接有间隔空隙 -> 左侧插入图片,drawableLeft -> 图片与文本直接间隔,drawablePadding
TextView 加边框
要给TextView 加边框 首先需要 写一个 ShapeDrawable 文件 -> 在drawable目录下新建一个xml文件 text_border
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#1d7dfa" />
<stroke
android:width="1px"
android:color="#ffffff" />
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
<corners
android:bottomLeftRadius="10px"
android:bottomRightRadius="10px"
android:topLeftRadius="10px"
android:topRightRadius="10px" />
</shape>
-> 标签 solid 设置 背景颜色
-> 标签 stroke 设置 边框的颜色与粗细
-> 标签 padding 设置 边距
-> 标签 corners 设置 圆角
效果如下
在这几个小的例子中我们又学到几个新的属性,也对xml标签属性的使用熟练了不少。但距离实际开发过程还差的远。需要在开发过程中不断积累和灵活使用,才可以应对丰富的需求场景。