返回介绍

TextSwitcher

发布于 2025-03-09 17:00:37 字数 3561 浏览 0 评论 0 收藏 0

TextSwitcher

版本:Android 2.2 r1

public class TextSwitcher extends ViewSwitcher

java.lang.Object

android.view.View

android.view.ViewGroup

android.widget.FrameLayout

android.widget.ViewAnimator

android.widget.ViewSwitcher

android.widget.TextSwitcher

概述

ViewSwitcher 仅仅包含子类型 TextView。TextSwitcher 被用来使屏幕上的 label 产生动画效果。每当 setText(CharSequence) 被调用时,TextSwitcher 使用动画方式将当前的文字内容消失并显示新的文字内容。

构造函数

public TextSwitcher (Context context)

创建一个新的空 TextSwitcher

参数

context 应用程序上下文

public TextSwitcher (Context context, AttributeSet attrs)

使用提供的 context 和 attributes 来创建一个空的 TextSwitcher

参数

context 应用程序环境

attrs 属性集合

公共方法

public void addView (View child, int index, ViewGroup.LayoutParams params)

根据指定的布局参数新增一个子视图

参数

child 新增的子视图

index 新增子视图的位置

params 新增子视图的布局参数

抛出异常

IllegalArgumentException 当子视图不是一个 TextView 实例时

public void setCurrentText (CharSequence text)

设置当前显示的文本视图的文字内容。非动画方式显示。

参数

text 需要显示的新文本内容

public void setText (CharSequence text)

设置下一视图的文本内容并切换到下一视图。可以动画的退出当前文本内容,显示下一文本内容。

参数

text 需要显示的新文本内容

示例

摘自 APIDemos->View->TextSwitcher

Java:

/**

* Uses a TextSwitcher.

*/

public class TextSwitcher1 extends Activity implements ViewSwitcher.ViewFactory,

View.OnClickListener {

private TextSwitcher mSwitcher;

private int mCounter = 0;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.text_switcher_1);

mSwitcher = (TextSwitcher) findViewById(R.id.switcher);

mSwitcher.setFactory(this);

Animation in = AnimationUtils.loadAnimation(this,

android.R.anim.fade_in);

Animation out = AnimationUtils.loadAnimation(this,

android.R.anim.fade_out);

mSwitcher.setInAnimation(in);

mSwitcher.setOutAnimation(out);

Button nextButton = (Button) findViewById(R.id.next);

nextButton.setOnClickListener(this);

updateCounter();

}

public void onClick(View v) {

mCounter++;

updateCounter();

}

private void updateCounter() {

mSwitcher.setText(String.valueOf(mCounter));

}

public View makeView() {

TextView t = new TextView(this);

t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);

t.setTextSize(36);

return t;

}

}

Xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<Button android:id="@+id/next"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/text_switcher_1_next_text" />

<TextSwitcher android:id="@+id/switcher"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

</LinearLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文