关闭

TextSwitcher为TextView实现平滑过渡

标签: TextSwitchertextview
258人阅读 评论(0) 收藏 举报

Xml文件:

<RelativeLayout 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"
    tools:context="cn.five.materialapp.FourFragment">
    <TextSwitcher
        android:id="@+id/switcher"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        >
    </TextSwitcher>
    <Button
        android:id="@+id/but_change"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="change"
        android:layout_below="@+id/switcher"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="8dp"
        />
</RelativeLayout>
代码:

public class FourFragment extends Fragment {
    private View view;
    private TextSwitcher switcher;
    private TextView tv;
    private Button change;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_four, container, false);
        initView();
        return view;
    }

    private void initView() {
        Animation in = AnimationUtils.loadAnimation(getContext(),android.R.anim.fade_in);
        Animation out = AnimationUtils.loadAnimation(getContext(),android.R.anim.fade_out);
        change = (Button) view.findViewById(R.id.but_change);
        switcher = (TextSwitcher) view.findViewById(R.id.switcher);
        switcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                tv = new TextView(getContext());
                tv.setGravity(Gravity.CENTER);

                tv.setText("这是一个TextViewSwitcher");
                return tv;
            }
        });
        switcher.setInAnimation(in);
        switcher.setOutAnimation(out);
        change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              switcher.setText("文字改变");
            }
        });
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3174次
    • 积分:130
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:2篇
    • 译文:1篇
    • 评论:1条
    文章存档
    最新评论