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原理相同

原文地址:https://www.cnblogs.com/chhom/p/4885756.html