Android中Tabhost的基本用法
下面是一个小实例
Layout界面代码:
<?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="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="wrap_content" android:layout_height="wrap_content"> </FrameLayout> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"/> </LinearLayout> </TabHost>
Activity代码
import android.app.TabActivity; import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; import android.widget.TabHost; /** * Created by Administrator on 2018/3/27. */ //显示选项卡组件的activity继承TabActivity public class MusicActivity extends TabActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.music); TabHost tabHost=getTabHost();//获取tabhost组件 TabHost.TabSpec tab1=tabHost.newTabSpec("page1").setIndicator("我的音乐").setContent(new Intent(this,MyMusicActivity.class)); tabHost.addTab(tab1); TabHost.TabSpec tab2=tabHost.newTabSpec("page2").setIndicator("所有歌曲").setContent(new Intent(this,AllMusicActivity.class)); tabHost.addTab(tab2); TabHost.TabSpec tab3=tabHost.newTabSpec("page3").setIndicator("播放列表").setContent(new Intent(this,OnMusicActivity.class)); tabHost.addTab(tab3); } }
对于每个选项卡中内容,可以在TabHost界面中通过设置组件来实现,也可以如上所示在其他activity中实现,通过intent进行跳转