TabHost简单使用

来源:互联网 发布:手机上的数据处理软件 编辑:程序博客网 时间:2024/04/30 21:10
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <LinearLayout        android:id="@+id/linear1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical" >        <TextView            android:id="@+id/tab_text"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="hello" />    </LinearLayout>    <LinearLayout        android:id="@+id/linear2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical" >        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="world" />    </LinearLayout>    <LinearLayout        android:id="@+id/linear3"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical" >    </LinearLayout></TabHost>



代码 :

private TextView txt;private View view;private TabHost tabhost;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);tabhost = getTabHost();// 设置使用TabHost布局view = LayoutInflater.from(this).inflate(R.layout.tableview,tabhost.getTabContentView(), true);// 添加标签页tabhost.addTab(tabhost.newTabSpec("tab1").setIndicator("hello").setContent(R.id.linear3));tabhost.addTab(tabhost.newTabSpec("tab2").setIndicator("world",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.linear2));// 当前选中,设置超出长度的默认为第一项tabhost.setCurrentTab(1);// 点击选项卡tabhost.setOnTabChangedListener(new OnTabChangeListener() {@Overridepublic void onTabChanged(String tabId) {tabhost.addTab(tabhost.newTabSpec("tab1").setIndicator("hello").setContent(R.id.linear1));// 清除tabhost.clearAllTabs();}});}

效果:


原创粉丝点击