android:为TextView添加样式、跑马灯、TextSwitcher和ImageSwitcher实现平滑过渡
一、样式
设置下划线:
textView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线
textView.getPaint().setAntiAlias(true);//抗锯齿
设置点击事件:
xml: android:clickable="true"
java: textView.setClickable(true);
textView.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0){
Uri uri=Uri.parse("tel:1111");
Intent intent=new Intent(Intent.ACTION_DIAL,uri);
startActivity(intnet);
}}
为TextView添加超链接
a:
String string="https://www.baidu.com/";
SpannableString spstring=new SpannableString(string);//设置超链接
spstring.setSpan(new URLSpan(spstring),0,string.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(string);
textView.setMovementMethod(LinkMvoementMethod.getInstance());
这样TextView就成了超链接方式,用户点击后就可以直接调用浏览器跳转到对应页面
b:
TextView tsyle01 = (TextView) findViewById(R.id.tsyle01);
String text="Visit <a href=\"http://manning.com/\">Manning home page</a>";
tsyle01.setText(Html.fromHtml(text));
tsyle01.setMovementMethod(LinkMovementMethod.getInstance());
为TextView添加加粗斜体显示
String string=“设置斜体”;
SpannableString sp=new SpannableString("设置斜体");//设置斜体
sp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),0,steing.length(),
Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
textView.setText(sp);
为不同字段设置不同样式:
TextView tstyle02=(TextView) findViewById(R.id.tstyle02);
String text01="Hello World,HomeActivity";
Spannable sText=new SpannableString(text01);
sText.setSpan(new BackgroundColorSpan(Color.RED),1,4,0);
sText.setSpan(new ForegroundColorSpan(Color.BLUE),5,9,0);
tstyle02.setText(sText);
二、跑马灯】
Android系统中TextView实现跑马灯效果,须具备以下几个条件:
1.android:ellipsize="marquee"
2.TextView必须单行显示,即内容必须超出TextView大小
3.TextView要获得焦点才能滚动
android:focusableInTouchMode="true"
android:focusable="true"
XML代码:
android:ellipsize="marquee"
android:singleLine="trye"
Java代码:
mText.setText("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa很长的数据");
mText.setSingleLine(true);
mText.setEllipsize(TruncateAt.MARQUEE); //让文字水平滑动
TextView还可设置跑马灯效果的滚动次数,如下:
XML代码设置:
android:marqueerpeatlimit="1" 1代表一次,-1代表无限循环
Java代码设置:
tText.setMarqueeRepeatLimit(-1);
但是这样子有一个缺点,就是这种状态的跑马灯只能在TextView处于焦点状态的时候,它才会滚动,对于实际的开发应用中很不实用,为了是跑马灯无论在什么情况下都能跑起来,这里需要自定义一个TextView,它继承TextView,并且重写isFocuse()方法,让它永远返回true,这样跑马灯效果就能一直的跑起来了。
public class MarqueeTextView extends TextView {
public MarqueeTextView(Context context) {
super(context);
}
public MarqueeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MarqueeTextView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean isFocused() {
return true;
}
}
在xml中引用
<com.sss.widget.view.MarqueeTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="@string/marquee_text1" />
三、TextSwitcher:
activity_main.xml:
<TextSwitcher
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="6sp"
android:textSize="20sp" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/click"
android:clickable="true"/>
fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="-100%"
android:toXDelta="0%"
android:duration="300"/>
</set>
faade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0%"
android:toXDelta="100%"
android:duration="300"/> </set>
MainActivity.java
public class MainActivity extends Activity implements ViewSwitcher.ViewFactory {
TextSwitcher t1;
Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1 = (TextSwitcher) findViewById(R.id.t1);
t1.setFactory(this);
Animation in = AnimationUtils.loadAnimation(this, R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(this, R.anim.fade_out);
t1.setInAnimation(in);
t1.setOutAnimation(out);
btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
t1.setText(String.valueOf(new Random().nextInt()));
}
});
}
@Override
public View makeView() {
TextView textView = new TextView(this);
return textView;
}
}
Randroid.R.anim.fade_in,这是一个淡入效果,也可以使用其他效果,步骤相同。ImageSwitcher和TextSwitcher原理相同
android:为TextView添加样式、跑马灯、TextSwitcher和ImageSwitcher实现平滑过渡的更多相关文章
- Hackfive 使用TextSwitcher和ImageSwitcher实现平滑过渡
1. 应用场景: 通过向左和向右的导航按钮浏览日期列表 在日期选择空间中改变日期 倒计时始终 新闻刚要 2.用到的知识点是: TextSwitcher和ImageSwitcher Te ...
- Android:TextView 自动滚动(跑马灯) (转)
Android:TextView 自动滚动(跑马灯) TextView实现文字滚动需要以下几个要点: 1.文字长度长于可显示范围:android:singleLine="true ...
- Android 文字自动滚动(跑马灯)效果的两种实现方法[特别好使]
有时候在xml中写的跑马灯效果不滚动:原因有以下 Android系统中TextView实现跑马灯效果,必须具备以下几个条件: 1.android:ellipsize=”marquee” 2.TextV ...
- TextView来实现跑马灯的效果
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- Android 实现多行文本跑马灯效果
Android TextView 实现跑马灯的效果很简单,只要加三个属性就可以了. android:ellipsize="marquee" android:focusable=&q ...
- TextView中实现跑马灯的最简单方法
几行代码实现跑马灯效果,效果如下: 因为很简单,所以就直接贴代码喽 <TextView android:id="@+id/item1_title_message" andro ...
- android 为TextView添加边框
今天需要在TextView上面添加一个边框,但是TextView本身不支持边框,所以只能采用其他方式,在网上查询了一下,主要有三种方式可以实现1.带有边框的透明图片2.使用xml的shape设置3继承 ...
- Android学习十二:跑马灯程序实现(简单联系)
package org.tonny; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; ...
- Android 为 TextView 添加超链接 (网址,邮件,电话)
<string name="info">Cette application a été développée par <a href="http://w ...
随机推荐
- Canal 实战 | 第一篇:SpringBoot 整合 Canal + RabbitMQ 实现监听 MySQL 数据库同步更新 Redis 缓存
一. Canal 简介 canal [kə'næl],译意为水道/管道/沟渠,主要用途是基于 MySQL 数据库增量日志解析,提供增量数据订阅和消费 早期阿里巴巴因为杭州和美国双机房部署,存在跨机房同 ...
- Git使用教程之初级入门命令行(二)
一.Git 操作流程图 1.git --help 查看帮助 Administrator@PC-xiaobing MINGW64 /d/Git (master) $ git --help usage: ...
- Mybatis:插入数据返回自增主键
使用Mybatis作为工具连接MySQL,要求在插入数据之后返回自增主键 一开始也很迷惑,Mybatis使用insert之后,成功返回的是1,失败会报错或返回0,主键去哪找来 后来知道Mybatis可 ...
- Sqlserver中判断表是否存在
在sqlserver(应该说在目前所有数据库产品)中创建一个资源如表,视图,存储过程中都要判断与创建的资源是否已经存在 在sqlserver中一般可通过查询sys.objects系统表来得知结果,不 ...
- Nginx的try_files指令使用实例
Nginx的配置语法灵活,可控制度非常高.在0.7以后的版本中加入了一个try_files指令,配合命名location,可以部分替代原本常用的rewrite配置方式,提高解析效率. try_file ...
- [atAGC052F]Tree Vertices XOR
结论 注意到如果$x$周围有偶数个1,对$x$操作显然不会改变$a_{x}$,因此不妨强制操作的点周围要有奇数个1,不难发现此时恰好会改变该点,即令$a_{x}=a_{x}\oplus 1$ 称$\{ ...
- [loj3504]支配
令$S_{x}$表示$x$支配的节点集合,可以暴力枚举$x$并求出$S_{x}$(删去$x$后从1开始dfs,复杂度为$o(nm)$),进而反过来即可求出受支配集$D_{x}$ 结论1:若$z\in ...
- 访问ajax请求后的结果
let getJPM = (function() { let result; let url ="xxx"; $.ajax({ type: "post", ur ...
- WebRTC与CSS滤镜(CSS filter)
我们知道了如何使用WebRTC打开摄像头,可以截取视频帧并且用canvas显示出来. 本文将滤镜与视频结合.给视频加上一层滤镜.主要使用到的是filter属性. canvas与滤镜 先来看filter ...
- Linux远程软件
Xhell6:Linux的终端模拟软件 1>安装并破解:解压.破解(运行两个.bat文件).启动(点击Xshell.exe文件) 2>连接远端Linux系统: 创建会话:点击连接,在常规框 ...