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 ...
随机推荐
- Spring Cloud Alibaba 使用 feign 和 rebion 进行服务消费
微服务的服务消费,一般是使用 feign 和 rebion 调用服务提供,进行服务的消费,本文将实战使用代码讲解服务的消费. 微服务环境的搭建 创建一个 springboot 项目,springboo ...
- CKAD认证中的部署教程
在上一章中,我们已经学会了使用 kubeadm 创建集群和加入新的节点,在本章中,将按照 CKAD 课程的方法重新部署一遍,实际上官方教程的内容不多,笔者写了两篇类似的部署方式,如果已经部署了 kub ...
- Electron快速入门之事件
const { app, BrowserWindow } = require('electron') function createWindow () { const win = new Brow ...
- LGV 引理小记
讲个笑话,NOI 之前某场模拟赛让我知道了这个神奇的科技,于是准备 NOI 之前学完,结果鸽着鸽着就鸽掉了,考 day1 之前一天本来准备花一天时间学的,然后我就开玩笑般地跟自己说,这么 trivia ...
- JOI 2020 Final 题解
T1. 只不过是长的领带 大水题,把 \(a_i,b_i\) 从小到大排序. 发现最优方案只可能是大的 \(a_i\) 跟大的 \(b_i\) 匹配,小的 \(a_i\) 与小的 \(b_i\) 匹配 ...
- Matlab指针数组
Matlab指针数组 前面博客Matlab指针中介绍了如何在Matlab中使用handle类型对象作为指针使用,本文则介绍一些使用这些类型指针的小技巧. 自定义类型的指针数组 在大部分编程语言中,我们 ...
- sigma网格中水平压力梯度误差及其修正
1.水平梯度误差产生 sigma坐标系下,笛卡尔坐标内水平梯度项对应形式为 \[\begin{equation} \left. \frac{\partial }{\partial x} \right| ...
- tabix 操作VCF文件
tabix 可以对NGS分析中常见格式的文件建立索引,从而加快访问速度,不仅支持VCF文件,还支持BED, GFF,SAM等格式. 下载地址: 1 https://sourceforge.net/pr ...
- Notepad++—显示代码对齐是使用了制表符还是空格
使用Notepad++打开脚本,勾选"显示空格与制表符",此时你会看到代码对齐使用了制表符与空格 右箭头:TAB:空格:点: 参考:https://www.cnblogs.com/ ...
- ggplot2 图例及分页参数
图例: 1 theme(legend.title =element_blank()) 2 guides(fill = guide_legend(title = NULL)) # 去掉图例title 3 ...