当前位置:   article > 正文

文本切换器的功能和用法_安卓文本切换器的作用

安卓文本切换器的作用

MainActivity.java

  1. package com.lxm.textswitchertest;
  2. import android.app.Activity;
  3. import android.graphics.Color;
  4. import android.os.Bundle;
  5. import android.view.Menu;
  6. import android.view.View;
  7. import android.widget.TextSwitcher;
  8. import android.widget.TextView;
  9. import android.widget.ViewSwitcher.ViewFactory;
  10. public class MainActivity extends Activity {
  11. private TextSwitcher textSwitcher;
  12. String[] strs = new String[]{"瘋狂Java講義","輕量級JavaEE企業應用實戰","瘋狂Android講義","瘋狂Ajax講義"};
  13. int curStr;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. textSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);
  19. textSwitcher.setFactory(new ViewFactory() {
  20. @Override
  21. public View makeView() {
  22. TextView tv = new TextView(MainActivity.this);
  23. tv.setTextSize(40);
  24. tv.setTextColor(Color.MAGENTA);
  25. return tv;
  26. }
  27. });
  28. next(null);
  29. }
  30. public void next(View v){
  31. textSwitcher.setText(strs[curStr++ % strs.length]);
  32. }
  33. @Override
  34. public boolean onCreateOptionsMenu(Menu menu) {
  35. // Inflate the menu; this adds items to the action bar if it is present.
  36. getMenuInflater().inflate(R.menu.main, menu);
  37. return true;
  38. }
  39. }

activity_main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity" >
  10. <TextSwitcher
  11. android:id="@+id/textSwitcher"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:onClick="next"/>
  15. </RelativeLayout>

运行效果:


声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号