Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android系列教程:TextSwitcher、Gallery、ImageSwitcher等控件用法

編輯:Android 開發教學

本文介紹在Android中使用各種控件(View),包括TextSwitcher、Gallery、ImageSwitcher、GridView、ListView與ExpandableList。

TextSwitcher——文字轉換器控件,改變文字時增加動畫效果;

Gallery——縮略圖瀏覽器控件;

ImageSwitcher——圖片轉換器控件,改變圖片時增加動畫效果;

GridView——網格控件;

ListView——列表控件;

ExpandableList——支援展開/收縮功能的列表控件。

1、TextSwitcher的Demo

textswitcher.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/btnChange" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="改變文字" />
<TextSwitcher android:id="@+id/textSwitcher"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>

_TextSwitcher.java

package com.webabcd.view;

import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;

public class _TextSwitcher extends Activity implements ViewSwitcher.ViewFactory {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.textswithcer);
setTitle("TextSwithcer");
final TextSwitcher switcher=(TextSwitcher) findViewById(R.id.textSwitcher);
// 指定轉換器的 ViewSwitcher.ViewFactory
switcher.setFactory(this);
// 設置淡入和淡出的動畫效果
Animation in=AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
Animation out=AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
switcher.setInAnimation(in);
switcher.setOutAnimation(out);
// 單擊一次按鈕改變一次文字
Button btnChange=(Button) this.findViewById(R.id.btnChange);
btnChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switcher.setText(String.valueOf(new Random().nextInt()));
}
});
}
// 重寫 ViewSwitcher.ViewFactory 的 makeView(),返回一個 View
@Override
public View makeView() {
TextView textView=new TextView(this);
textView.setTextSize(36);
return textView;
}
}

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved