切换卡的应用,切换卡的应用较广,可以充分的利用有限的空间,如上面所示,就是个切换卡的布局
切换卡的XML的布局文件主要分为三大部分
1. TabHost整个的容器 tabhost
2. TabWidget切换卡标题 tabs
3. Tab的内容 tabcontent
xml代码如下:
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tv1" android:text="The first tab" /> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tv2" android:text="The second tab" /> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tv3" android:text="The third tab" /> </FrameLayout> </LinearLayout> </TabHost>
Java代码实现:
public class TabWidgetActivity extends TabActivity implements OnTabChangeListener{
TabHost host;
@Override
protected 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));
//指定当前选中的是Tab
host.setCurrentTab(0);
host.setOnTabChangedListener(this);
}
//变化时的事件
@Override
public 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() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.setIcon(R.drawable.error);
dialog.show();
}
}