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

Android 控件实战:TabHost、Gallery、ListView 與 ImageSwitcher

編輯:關於 Android 程式設計

本文透過一個綜合範例,帶你掌握 TabHost、Gallery、ListView 與 ImageSwitcher 四個常用控制項的搭配使用,打造一個具備分頁、相冊瀏覽與評分功能的應用。

一、TabHost 分頁控件

TabHost 又稱分頁控件,在多種開發語言中都存在。它可以容納多個標籤頁,每個標籤頁可放置不同的內容——在 Android 中,一個標籤頁可以放一個 View 或一個 Activity。TabHost 是標籤控件的核心類,也是標籤的集合。

布局檔中,TabHost 內部通常包含一個 TabWidget(負責顯示標籤)與一個 FrameLayout(負責顯示內容):

<TabHost android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TabWidget android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <LinearLayout android:id="@+id/tab1" ... />
            <LinearLayout android:id="@+id/tab2" ... />
            <LinearLayout android:id="@+id/tab3" ... />
        </FrameLayout>
    </LinearLayout>
</TabHost>

在後台,可透過 addTab 方法添加分頁。本例添加了 3 個標籤,並為其中兩個綁定不同的 Activity:

TabHost tabhost = this.getTabHost();
LayoutInflater.from(this).inflate(R.layout.activity_main, tabhost.getTabContentView(), true);
tabhost.addTab(tabhost.newTabSpec("tab1").setIndicator("撥號").setContent(new Intent(this, MyGallery.class)));
tabhost.addTab(tabhost.newTabSpec("tab2").setIndicator("記錄").setContent(new Intent(this, RatingListActivity.class)));
tabhost.addTab(tabhost.newTabSpec("tab3").setIndicator("聯系人").setContent(R.id.tab3));

二、Gallery 與 ImageSwitcher

Gallery 是相冊控件,可水平或垂直瀏覽多張圖片;ImageSwitcher 則能以動畫方式切換圖片。本例將兩者結合:Gallery 存放多張圖片,ImageSwitcher 負責顯示。

Gallery gallery;
ImageSwitcher imgwch;
private int[] imgIds = {R.drawable.imgs1, R.drawable.imgs2, R.drawable.imgs3,
    R.drawable.imgs4, R.drawable.imgs5, R.drawable.imgs6, R.drawable.imgs7};

public void onCreate(Bundle savebundle) {
    super.onCreate(savebundle);
    setContentView(R.layout.imgswitch);
    imgwch = (ImageSwitcher) findViewById(R.id.imgswitcher1);
    imgwch.setFactory(this);
    // 設定淡入淡出動畫
    imgwch.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
    imgwch.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
    gallery = (Gallery) findViewById(R.id.gallery);
    ImageAdapter adapter = new ImageAdapter(this);
    gallery.setAdapter(adapter);
    gallery.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            // 使用取餘運算實現循環顯示
            imgwch.setBackgroundResource(imgIds[arg2 % imgIds.length]);
        }
    });
}

// 定義 ImageSwitcher 的顯示物件
public View makeView() {
    ImageView imageView = new ImageView(this);
    imageView.setBackgroundColor(Color.TRANSPARENT);
    imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
    imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    return imageView;
}

其中 ImageAdapter 繼承 BaseAdapter,getCount() 返回 Integer.MAX_VALUE 以实现循环浏览,getView() 则设定每个图片的显示风格与资源。通过点击「撥號」标签,即可显示 Gallery 页,ImageSwitcher 也会随 Gallery 浏览对象的变化而切换。

三、RatingList:ListView 与 RatingBar 评分

本例实现一个基于 RatingBar 与 ListView 的评分应用。列表由 ListView 构成,其 item 布局包含图标、应用名称、作者与评分栏:

<ListView android:id="@+id/lvrating"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
</ListView>

在 Activity 中为 ListView 绑定数据,并设置监听事件。点击某个 item 时弹出对话框,让用户为其打分:

ListView listview = (ListView) findViewById(R.id.lvrating);
final MyBaseadapter adapter = new MyBaseadapter(this);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {
        View myView = getLayoutInflater().inflate(R.layout.rating, null);
        final RatingBar ratingbar = (RatingBar) myView.findViewById(R.id.ratingBar1);
        ratingbar.setRating(applicationrating[arg2]);
        new AlertDialog.Builder(RatingListActivity.this)
            .setTitle(applicationNames[arg2])
            .setMessage("給城市打分")
            .setView(myView)
            .setPositiveButton("確定", new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    adapter.setRating(arg2, ratingbar.getRating());
                }
            })
            .setNegativeButton("取消", null).show();
    }
});

MyBaseadapter 继承 BaseAdapter,其 getView 方法中透过 convertView 复用 item 布局,为每个控件赋值后返回。这里最需要注意的是 LayoutInflater.inflate 的用法——获取到 View 对象后,再为其中的每个控件赋值,最后将整个 View 返回,这个 View 就是 ListView 的每一个 item。

透過這個綜合範例,你已能將這四個常用控件靈活組合,構建出功能豐富的 Android 介面。

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