編輯:關於 Android 程式設計
本文透過一個綜合範例,帶你掌握 TabHost、Gallery、ListView 與 ImageSwitcher 四個常用控制項的搭配使用,打造一個具備分頁、相冊瀏覽與評分功能的應用。
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 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 浏览对象的变化而切换。
本例实现一个基于 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 介面。
Android 自定義 View 實現圓形與半圓形菜單詳解
前不久看到鴻洋大大的圓形菜單,實在太酷了,於是萌生模仿的念頭。結合朋友給我講的一些思路與分析,我便開始改造自己的圓形菜單。 文章結構:1. 功能介紹與展示;2. 部分代碼
攜手共譯《Embedded Android》:開源社區協作翻譯招募
如果你已經具備一定的 Android 開發基礎,也讀得懂英文技術文獻,並且懷抱著一份為開源社區貢獻力量的熱忱,那麼這裡有一個不錯的機會正等著你。 我們計劃以協作的方式,共
Android 線程池核心:ThreadPoolExecutor 源碼全解析
通俗點說,線程池就是「任務隊列 + 線程」的組合。接下來我們一起拆解 ThreadPoolExecutor 的源碼。 先看一眼類圖,對 ThreadPoolExecuto
Android Studio 基本設定整理
Android Studio 的設定看似瑣碎,卻會直接影響日常開發手感。以下整理幾個最常被調整的項目,方便快速建立適合自己的開發環境。進入設定頁面可以從選單進入 Sett