@gogogodeng
2017-08-10T05:43:48.000000Z
字数 2635
阅读 487
android控件
tabhost就是一个tab选项卡切换控件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TabHostandroid:layout_width="match_parent"android:layout_height="wrap_content" android:id="@+id/tabHost" android:layout_gravity="center_horizontal"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TabWidgetandroid:id="@android:id/tabs"android:layout_width="match_parent"android:layout_height="match_parent"></TabWidget><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:id="@+id/tab1"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/imageView"android:src="@drawable/loll01"android:layout_gravity="center"/></LinearLayout><LinearLayoutandroid:id="@+id/tab2"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/imageView10"android:src="@drawable/loll02"android:layout_gravity="center"/></LinearLayout><LinearLayoutandroid:id="@+id/tab3"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/imageView11"android:src="@drawable/loll03"android:layout_gravity="center"/></LinearLayout></FrameLayout></LinearLayout></TabHost></LinearLayout>
其中tabs和tabcontent这2个id不能更改
package com.testapp.app;import android.app.Activity;import android.os.Bundle;import android.widget.TabHost;/*** Created by develop on 2015/11/17.*/public class TabHostActivity extends Activity {private TabHost tabHost;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.tabhost);tabHost = (TabHost) this.findViewById(R.id.tabHost);tabHost.setup();tabHost.addTab(tabHost.newTabSpec("tab_1").setContent(R.id.tab1).setIndicator("TAB1", null));tabHost.addTab(tabHost.newTabSpec("tab_2").setContent(R.id.tab2).setIndicator("TAB2", null));tabHost.addTab(tabHost.newTabSpec("tab_3").setContent(R.id.tab3).setIndicator("TAB3", null));tabHost.setCurrentTab(0);/*** 选项卡切换事件*/tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {@Overridepublic void onTabChanged(String tabId) {}});}}
