Textswitcher的简单用法与功能(文本切换器)

这篇博客介绍了如何利用TextSwitcher组件创建一个简单的文本切换动画效果,当点击文本时,内容会以左进右出的方式进行切换。示例代码展示了在布局文件中配置动画的实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个简单的文本切换动画效果案例。点击文本是,文本左进右出。
这里写图片描述
代码如下

package com.test.textswitcher;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.TextureView;
import android.view.View;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity {
    //声明控件
    private TextSwitcher textSwitcher;
    //为TextSwitcher提供String文本
    int curStr;
    String [] strs = new String[]{
            "android","ios","java","c++"
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.testseitcher_main);
        //实例化控件
        textSwitcher = (TextSwitcher)findViewById(R.id.textswitcher);
        //设置文本切换动画
        textSwitcher.setFactory(new ViewFactory() {

            @Override
            public View makeView() {
                TextView tv = new TextView(MainActivity.this);
                tv.setTextSize(40);
                tv.setTextColor(Color.BLUE);
                return tv;
            }
        });
        next (null);
    }
    public void next(View source){
        textSwitcher.setText(strs[curStr++ % strs.length]);
    }
}

一个简单布局如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <TextSwitcher android:id="@+id/textswitcher"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inAnimation="@android:anim/slide_in_left"
        android:outAnimation="@android:anim/slide_out_right"
        android:onClick="next" />

</LinearLayout>

这里的动画效果比较简单的系统自带的效果,在布局中就已经定义好了,无需在代码中体现。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值