Android UI基础——TextSwitcher&ImageSwitcher控件
2016-04-04 15:49
549 查看
TextSwitcher
TextSwitcher是一个文字交换器控件,其继承关系如图:在布局的xml文件中的创建也很简单,都是最基本的属性,如下:
<TextSwitcher android:id="@+id/text_switcher" android:layout_width="match_parent" android:layout_height="wrap_content"/>
此控件的重点在于如何在类中去实现文字交换,其步骤如下:
①得到TextSwitcher实例对象 TextSwitcher mTextSwitcher= (TextSwitcher) findViewById(R.id.textSwitcher);
②为switcher指定ViewSwitcher.ViewFactory工厂,该工厂会产生出转换时需要的View mTextSwitcher.setFactory(this);
③为switcher设定显示的内容,该方法执行,就会切换到下个View mTextSwitcher.setText();
其中 要实现ViewSwitcher.ViewFactory中的makeView()方法 。
具体代码如下(此代码实现的是点击按钮后文字变化加1):
public class MainActivity extends AppCompatActivity implements ViewSwitcher.ViewFactory { private TextSwitcher mTextSwitcher; private int i = 0; private Button mButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取TextSwitcher控件 mTextSwitcher = (TextSwitcher) findViewById(R.id.text_switcher); //为实例化对象指定工厂,该工场会产生出转换时需要的View mTextSwitcher.setFactory(this); mButton = (Button) findViewById(R.id.button_text); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { i++; //设定显示内容,该方法执行就会切换到下个View mTextSwitcher.setText(i+""); } }); } @Override public View makeView() { TextView textView = new TextView(this); textView.setText(i+""); textView.setGravity(Gravity.CENTER); return textView; } }
效果图如下:
ImageSwitcher
ImageSwitcher是一个图片切换器,和ImageView相比,多了一个功能,那就是它说显示的图片切换时,可以设置动画效果。其继承关系如下:因为和TextSwitcher继承与同一个类,使用方式与TextSwitcher基本相似,唯一不同点在于我们需要在代码中设置图片显示大小。所以这边只写一下设置图片显示大小的代码,其他大家自己可以对照TextSwitcher的代码练习练习,如下:
public View makeView() { ImageView imageView = new ImageView(this); imageView.setLayoutParams(new ImageSwitcher.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); return imageView; }
在布局的xml文件中的属性基本和ImageView属性相似。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories