您的位置:首页 > 移动开发 > Android开发

Demo for Android Tab Widget

2012-11-21 00:51 267 查看
按照Google Android SDK的例子,做了tab view的练习,其中破折颇多。

先是layout的图形界面无法显示TabWidget (TabHost);再是切换Tab的时候图标并没有变化;还有git的TGitCache导致CPU过高,机器发热大,死机的问题。为了解决第一个问题,更新了SDK,还有Eclipse IDE中的更新,但是却出现第二次运行(Run)只到27%的问题。查了以后才知道,这个bug需要先运行模拟器,然后再run就可以了。

通过练习,敲代码,会加深不少印象,在这个过程中,遇到了知识点就去补充。虽然可能会记不住,很快忘掉,但是先在脑子里留个印象对以后再来会有好处。

贴一下关键文件的代码,自行查找知识点,下载完整代码请点击:iBuoTabView

package ian.study.demo;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.Toast;
import android.util.Log;

public class HelloTabWidget extends TabActivity  implements OnTabChangeListener{
public static final String TAG = "TabDemo";

private TabHost mTabHost = null;  // The activity TabHost
private static final int TAB_ARTISTS = 0;
private static final int TAB_ALBUMS  = 1;
private static final int TAB_SONGS   = 2;
private static final int TAB_MORE    = 3;

private static final int TAB_ID    = 0;
private static final int TAB_TITLE = 1;

private static final String[][] mTabTag={
{"artists", "Artists"},
{"albums",  "Albums"},
{"songs",   "Songs"},
{"more",    "..."},
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mTabHost = getTabHost();

Resources res = getResources(); // Resource object to get Drawables

TabHost.TabSpec spec;  // Resusable TabSpec for each tab
Intent intent;  // Reusable Intent for each tab

// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, ArtistsActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = mTabHost.newTabSpec(mTabTag[TAB_ARTISTS][TAB_ID]).
setIndicator(mTabTag[TAB_ARTISTS][TAB_TITLE],
res.getDrawable(R.drawable.ic_tab_artists)).setContent(intent);

mTabHost.addTab(spec);

// Do the same for the other tabs
intent = new Intent().setClass(this, AlbumsActivity.class);
spec = mTabHost.newTabSpec(mTabTag[TAB_ALBUMS][TAB_ID]).
setIndicator(mTabTag[TAB_ALBUMS][TAB_TITLE],
res.getDrawable(R.drawable.ic_tab_albums))
.setContent(intent);
mTabHost.addTab(spec);

intent = new Intent().setClass(this, SongsActivity.class);
spec = mTabHost.newTabSpec(mTabTag[TAB_SONGS][TAB_ID]).
setIndicator(mTabTag[TAB_SONGS][TAB_TITLE],
res.getDrawable(R.drawable.ic_tab_songs))
.setContent(intent);
mTabHost.addTab(spec);

/*
* add one more tab for testing Drawable in code. */
StateListDrawable drawIcon;
drawIcon = new StateListDrawable();
drawIcon.addState(new int[]
{android.R.attr.state_enabled},
res.getDrawable(R.drawable.icon));
intent = new Intent().setClass(this, SongsActivity.class);
spec = mTabHost.newTabSpec(mTabTag[TAB_MORE][TAB_ID]).
setIndicator(mTabTag[TAB_MORE][TAB_TITLE],
drawIcon).setContent(intent);
mTabHost.addTab(spec);

mTabHost.setCurrentTab(2);

mTabHost.setOnTabChangedListener(this);
}

public void onTabChanged(String tabId) {
Activity activity = getLocalActivityManager().getActivity(tabId);

Log.d(TAG, "tabId=" + tabId);
Toast.makeText(this, tabId, Toast.LENGTH_SHORT).show();

if((tabId == mTabTag[TAB_MORE][TAB_ID]) && (activity != null))
{
/* do something when focus to info tab
* Just create a new TextView and set its text. */
TextView textview = new TextView(activity);
textview.setText(getString(R.string.app_name) + " by Ian@blog.ibuo.org");

activity.setContentView(textview);
}

}
}

Posted by ian at 06:45 Tagged with: Android, View, Widget
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: