读书人

TabWidget的使用

发布时间: 2012-08-27 21:21:56 作者: rapoo

TabWidget的应用

切换卡的应用,切换卡的应用较广,可以充分的利用有限的空间,如上面所示,就是个切换卡的布局

切换卡的XML的布局文件主要分为三大部分

1.TabHost整个的容器 tabhost

2.TabWidget切换卡标题 tabs

3.Tab的内容tabcontent

?

xml代码如下:

?

?

?

Java代码实现:

?

?

public class TabWidgetActivity extends TabActivity implements OnTabChangeListener{TabHost host;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.tab_main);host = getTabHost();//对其进行选项卡的添加,tab1为定义的选项卡的引用,不要和其它的选项卡引用重复,不然不好监听对应的事件host.addTab(host.newTabSpec("tab1").setIndicator("Tab1", this.getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tv1));host.addTab(host.newTabSpec("tab2").setIndicator("Tab2", this.getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tv2));host.addTab(host.newTabSpec("tab3").setIndicator("Tab3", this.getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tv3));//指定当前选中的是Tabhost.setCurrentTab(0);host.setOnTabChangedListener(this);}//变化时的事件@Overridepublic void onTabChanged(String tabId) {LinearLayout ly = new LinearLayout(this);LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);ly.setLayoutParams(lp);SeekBar seek = new SeekBar(this);seek.setMax(100);seek.setProgress(50);seek.setSecondaryProgress(75);seek.setLayoutParams(lp);ly.addView(seek);AlertDialog dialog = new AlertDialog.Builder(this).create();dialog.setView(ly);dialog.setButton("OK", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {}});dialog.setIcon(R.drawable.error);dialog.show();}}
?

?

读书人网 >移动开发

热点推荐