タブを使って内容(コンテンツ部)を埋め込む方法を紹介する。タブとコンテンツプログラムで生成して好きなだけ並べることができる。
基本的なタブの使い方は 「tabLayoutでタブを小さくして、icon非表示にして左側に配置する方法」 を見ると良い。
tab.setContentで「TabHost.TabContentFactory ()」で作ったコンテンツのviewをセットする。「tab_content」というlayoutファイルを作ってそれを利用している。
ここではさらにタブも独自のものを作って使っている。自由にスタイルを変更できるので、画面の小さいスマホに合わせるとすごくいい。
//TabHostクラスのインスタンス生成
TabHost tabHost = getTabHost();
i=0;
//String[] arycategories = getResources().getStringArray(R.array.categories);
for (i=0; i<arycategories.length; i++) {
TabSpec tab = tabHost.newTabSpec(String.valueOf(i+1));
View view = View.inflate(getApplication(), R.layout.tab_view, null);
TextView text = (TextView)view.findViewById(R.id.TextView01);
text.setText(String.valueOf(i+1));
tab.setIndicator(view);
//コンテンツを埋め込む部分
tab.setContent(new TabHost.TabContentFactory (){
int t = i+1;
public View createTabContent(String tag) {
View contentview = View.inflate(getApplication(), R.layout.tab_content, null);
LinearLayout chart_area = (LinearLayout)contentview.findViewById(R.id.chart_area);
BaseChart chart = new BaseChart(t);
chart_area.addView(chart.execute(getApplication()));
return contentview;
}
});
tabHost.addTab(tab);
}