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 ...
随机推荐
- 折腾systemd-nspawn运行centos7
Archlinux创建Debian/Ubuntu的systemd-nspawn容器是很简单的,因为有debootstrap软件.某天我突然想装个centos7玩玩,搜了半天没发现有什么类似于deboo ...
- TTMS 一个基于Java Swing的Socket通信的剧院票务管理系统
TTMS (Theater Ticket Management System) 点我进入github TTMS全称剧院票务管理系统,分为客户端和服务器端.服务器端可以接收客户端连接请求,客户端相当于我 ...
- Git - git push origin master 报错的解决方法
亲测实用,转载保存,原文地址:https://blog.csdn.net/kangvcar/article/details/72773904 错误提示如下: [root@linux1 php]# gi ...
- 设计模式学习-使用go实现适配器模式
适配器模式 定义 代码实现 优点 缺点 适用范围 代理.桥接.装饰器.适配器4种设计模式的区别 参考 适配器模式 定义 适配器模式的英文翻译是Adapter Design Pattern.顾名思义,这 ...
- 问题 Q: 最大的数
题目描述 小明和小红在打赌说自己数学学的好,于是小花就给他们出题了,考考他们谁NB,题目是这样的给你N个数 在这n个数之间添加N-1个*或+,使结果最大,但不可以打乱原顺序,请得出这个结果 如 1 3 ...
- ES6--ES12笔记整理(1)
一.let const 五个共同特点 不允许重复声明 块级作用域 不存在变量提升 不影响作用域链 暂时性死区---在代码块内,使用let/const命令声明变量之前,该变量都是不可用的.这在语法上,称 ...
- SpringCloud微服务实战——搭建企业级开发框架(十九):Gateway使用knife4j聚合微服务文档
本章介绍Spring Cloud Gateway网关如何集成knife4j,通过网关聚合所有的Swagger微服务文档 1.gitegg-gateway中引入knife4j依赖,如果没有后端代码编 ...
- [atARC123F]Insert Addition
前置知识 下面,先来介绍一下Stern-Brocot Tree的结构: 其是一棵满二叉树,每一个节点都是一个最简分数,其中根为$\frac{1}{1}$ 假设前$i$层的中序遍历分数依次为$\frac ...
- sb 错误
数组开小.很容易 \(2 \times 10^5\) 或 \(10^6\) 就开成 \(10^5\),或者各种变量的数据范围混用,\(m \leq 5\times 10^5\),结果只开到了 \(n\ ...
- Atcoder Regular Contest 092 D - Two Faced Edges(图论+bitset 优化)
Atcoder 题面传送门 & 洛谷题面传送门 orz ymx,ymx ddw %%% 首先既然题目要我们判断强连通分量个数是否改变,我们首先就将原图 SCC 缩个点呗,缩完点后我们很自然地将 ...