Android中TabHost如何使用,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
<?xml version="1.0" encoding="UTF-8"?>
<!-- Tab导航 最新版 -->
<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" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFF" >
</FrameLayout>
<!-- TabWidget管理所有的选项卡,id名是android指定的 -->
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone" />
<!-- Frame下放置单选群组替代TAB效果 -->
<RadioGroup
android:id="@+id/main_radio"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ccc"
android:gravity="center_vertical"
android:orientation="horizontal"
android:layout_alignParentBottom="true" >
<RadioButton
android:id="@+id/tab_icon_qq"
android:checked="true"
android:drawableTop="@drawable/message"
android:text="消息" />
<RadioButton
android:id="@+id/tab_icon_address"
android:checked="false"
android:drawableTop="@drawable/home"
android:text="班级" />
<RadioButton
android:id="@+id/tab_icon_myself"
android:drawableTop="@drawable/d1"
android:checked="false"
android:text="我" />
</RadioGroup>
</RelativeLayout>
</TabHost>
MainActivity
public class MainActivity extends TabActivity {
public static TabHost mTabHost;
public static TabHost getmTabHost() {
return mTabHost;
}
private RadioGroup main_radio;
private RadioButton tab_icon_qq, tab_icon_address,tab_icon_myself;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = getTabHost();
final TabWidget tabWidget = mTabHost.getTabWidget();
tabWidget.setStripEnabled(false);
mTabHost.addTab(mTabHost.newTabSpec("TAG1").setIndicator("0").setContent(new Intent(this,MessageActivity.class)));
mTabHost.addTab(mTabHost.newTabSpec("TAG2").setIndicator("1").setContent(new Intent(this,ClassActivity.class)));
mTabHost.addTab(mTabHost.newTabSpec("TAG3").setIndicator("2").setContent(new Intent(this,MyselfActivity.class)));
main_radio = (RadioGroup) findViewById(R.id.main_radio);
tab_icon_weixin = (RadioButton) findViewById(R.id.tab_icon_weixin);
tab_icon_address = (RadioButton) findViewById(R.id.tab_icon_address);
tab_icon_myself = (RadioButton) findViewById(R.id.tab_icon_myself);
main_radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int id) {
if (id == tab_icon_weixin.getId()) {
mTabHost.setCurrentTab(0);
} else if (id == tab_icon_address.getId()) {
mTabHost.setCurrentTab(1);
} else if (id == tab_icon_myself.getId()) {
mTabHost.setCurrentTab(2);
}
}
});
// 设置当前显示哪一个标签
mTabHost.setCurrentTab(0);
// 遍历tabWidget每个标签,
for (int i = 0; i < tabWidget.getChildCount(); i++) {
View vv = tabWidget.getChildAt(i);
vv.getLayoutParams().height = 45;
// vv.getLayoutParams().width = 65;
vv.setBackgroundColor(Color.BLUE);
}
// findViewById(R.id.tab_icon_brand).setOnClickListener(this);
}
}
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。