最近由于项目原因,需要使用tabhost,写一下自己的使用方法
实现TabHost有三种方式:继承自TabActivity,ActivityGroup和自定义的Activity
1.使用TabAcitvity
TabActivity他自己包含一个Tabhost,我们通过getTabhost(),也不需要调用setContentView()设置layout。如果设置一定要按照android SDK的规定进行设置。SDK规定的是:TabHost,TabWidget,FrameLayout的id必须为@android:id/tabhost,@android:id/tabs
,@android:id/tabcontent;
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_height="match_parent"
android:layout_width="fill_parent" android:background="@drawable/inde_bg">
<LinearLayout android:orientation="vertical" android:id="@+id/layout1"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TabWidget>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</FrameLayout>
</LinearLayout>
</TabHost>
否则会报什么需要设置相应的id。
实现代码如下(需要的图片包括在zip文件中):
public class IndexActicity extends TabActivity {
private int index_tab = 0;
private TabWidget tabWidget;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
Constant.WIDTH = dm.widthPixels;
Constant.HEIGHT= dm.heightPixels;
TabHost t =getTabHost();
t.setBackgroundDrawable(getResources().getDrawable(R.drawable.inde_bg));
t.setPadding(0, 0, 0, 0);
tabWidget = t.getTabWidget();
LayoutInflater fi = LayoutInflater.from(IndexActicity.this);
View view = fi.inflate(R.layout.tab_layout, null);
LinearLayout ll = (LinearLayout) view.findViewById(R.id.tablayout);
View tab1 = view.findViewById(R.id.tab1);
View tab2 = view.findViewById(R.id.tab2);
View tab3 = view.findViewById(R.id.tab3);
View tab4 = view.findViewById(R.id.tab4);
ll.removeAllViews();
t.addTab(t.newTabSpec("1").setIndicator(tab1
).setContent(
new Intent(IndexActicity.this,
TabActivity1.class)));
t.addTab(t.newTabSpec("2").setIndicator(tab2
).setContent(
new Intent(IndexActicity.this,
TabActivity2.class)));
t.addTab(t.newTabSpec("3").setIndicator(tab3
).setContent(
new Intent(IndexActicity.this,
TabActivity3.class)));
t.addTab(t.newTabSpec("4").setIndicator(tab4).setContent(
new Intent(IndexActicity.this, TabActivity4.class)));
tabWidget.setBackgroundColor(getResources().getColor(R.color.alpha_00));
tabWidget.setBaselineAligned(true);
tab1.setBackgroundDrawable(getResources().getDrawable(
R.drawable.menu_bg));
for (int i = 0; i < tabWidget.getChildCount(); i++) {
tabWidget.getChildAt(i).getLayoutParams().width = Constant.WIDTH / 4;
tabWidget.getChildAt(i).getLayoutParams().height=50;
}
t.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
tabChanged(tabId);
}
});
}
//捕获tab变化事件
public void tabChanged(String tabId) {
if (index_tab != (Integer.valueOf(tabId) - 1)) {
tabWidget.getChildAt(Integer.valueOf(tabId) - 1)
.setBackgroundDrawable(
getResources().getDrawable(R.drawable.menu_bg));
tabWidget.getChildAt(index_tab).setBackgroundDrawable(null);
index_tab = Integer.valueOf(tabId) - 1;
}
}
}
运行结构如图1
[img]
[/img]
备注:我在继承TabActivity的时候,将TabWidget的android:layout_height设置为"fill_parent"时,结构出现的图为
[img]
[/img]
2.使用GroupActivity和自定义
使用GroupActivity和自己定义的Activity的区别不大,主要区别是:GroupActivity主要为了实现每个Tab可以包含不同的Activity,而自己定义的Activity却不能实现,只能显示一些View。所以如果你需要在不同的tab中显示Activity时,则需要继承GroupActivity,或者TabActivity。
用这两种方法实现需要调用setContentView()设置layout,layout中包括这个tabhost,tabwidget及Fremelayout。而且一定要对tabhost调用setup方法,否则会出现空指针异常,原因是setup方法初始化tabwidget和framelayout对象。
这个时候的layout如下:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/testhost" android:layout_height="match_parent"
android:layout_width="fill_parent" android:background="@drawable/inde_bg">
<LinearLayout android:orientation="vertical" android:id="@+id/layout1"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TabWidget>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</FrameLayout>
</LinearLayout>
</TabHost>
类如下
public class IndexActicity extends ActivityGroup {
private int index_tab = 0;
private TabWidget tabWidget;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.index_layout);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
Constant.WIDTH = dm.widthPixels;
Constant.HEIGHT= dm.heightPixels;
//TabHost t =getTabHost();
TabHost t = (TabHost)findViewById(R.id.testhost);
t.setBackgroundDrawable(getResources().getDrawable(R.drawable.inde_bg));
t.setup(this.getLocalActivityManager());
t.setPadding(0, 0, 0, 0);
tabWidget = t.getTabWidget();
LayoutInflater fi = LayoutInflater.from(IndexActicity.this);
View view = fi.inflate(R.layout.tab_layout, null);
LinearLayout ll = (LinearLayout) view.findViewById(R.id.tablayout);
View tab1 = view.findViewById(R.id.tab1);
View tab2 = view.findViewById(R.id.tab2);
View tab3 = view.findViewById(R.id.tab3);
View tab4 = view.findViewById(R.id.tab4);
ll.removeAllViews();
t.addTab(t.newTabSpec("1").setIndicator(tab1
).setContent(
new Intent(IndexActicity.this,
TabActivity1.class)));
t.addTab(t.newTabSpec("2").setIndicator(tab2
).setContent(
new Intent(IndexActicity.this,
TabActivity2.class)));
t.addTab(t.newTabSpec("3").setIndicator(tab3
).setContent(
new Intent(IndexActicity.this,
TabActivity3.class)));
t.addTab(t.newTabSpec("4").setIndicator(tab4).setContent(
new Intent(IndexActicity.this, TabActivity4.class)));
tabWidget.setBackgroundColor(getResources().getColor(R.color.alpha_00));
tabWidget.setBaselineAligned(true);
tab1.setBackgroundDrawable(getResources().getDrawable(
R.drawable.menu_bg));
for (int i = 0; i < tabWidget.getChildCount(); i++) {
tabWidget.getChildAt(i).getLayoutParams().width = Constant.WIDTH / 4;
tabWidget.getChildAt(i).getLayoutParams().height=50;
}
t.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
tabChanged(tabId);
}
});
}
//捕获tab变化事件
public void tabChanged(String tabId) {
if (index_tab != (Integer.valueOf(tabId) - 1)) {
tabWidget.getChildAt(Integer.valueOf(tabId) - 1)
.setBackgroundDrawable(
getResources().getDrawable(R.drawable.menu_bg));
tabWidget.getChildAt(index_tab).setBackgroundDrawable(null);
index_tab = Integer.valueOf(tabId) - 1;
}
}
细心的人可能看到,这个layout中的tabhost 的属性id不是android默认的tabhost了,这个时候,就需要在activity中使用findViewById(R.id.XXXX);当然你也可以使用android的默认id,在类中直接使用:TabHost t = (TabHost)findViewById(android.R.id.tabhost);但是如果你修改了tabhost的属性id,而在调用的时候又使用默认的id,则会报找不到android.R.id.tabhost标签。
如果修改了tabwidget和framelayout的id,系统会报需要相应的标签,这个是在setup方法调用的默认id。因为他只会在当前的layout中找android.R.id.tabs和android.R.id.tabcontent。
如果想让tab显示在下边,只需要将tabwidget和framelayout调换位置即可。
试着修改了一下tabwidget和framelayout的属性id值,但是没有找到解决办法,哪位大牛如果知道,可以告诉一下,
- 大小: 69.3 KB
- 大小: 73.8 KB
分享到:
相关推荐
android tabhost --android UI源码.rar
Androidtabhost--AndroidUI源码.zip
android tabhost --android UI源码.zip
Android源码——android tabhost --android UI源码.zip
android tabhost --android UI源码.zip项目安卓应用源码下载android tabhost --android UI源码.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考
tabhost--UI源码.zip
安卓Android源码——tabhost--UI源码.zip
前几年的Android应用源码Demo,主要面向的是学生毕业设计学习。
android tabhost --android UI源码 Android Txt文本阅读器源码 Android Widget快捷拨号程序源码 Android 仓库管理系统源码 Android 仿ES界面文件浏览器源码 Android 仿iPhoneQQ气泡聊天样式源码 Android 仿QQ多级...
Android TabHost实现屏幕UI窗口的TAB切换功能,实现的过程:声明TabHost对象、取得TabHost对象,为TabHost添加标签,新建一个newTabSpec(newTabSpec),设置其标签和图标(setIndicator),设置内容(setContent),然后...
Android,TabHos的UI设计,菜单效果完美显示,模板作用。
android TabHost全用法,欢迎下载学习交流
Android TabHost嵌套示例,像电脑端一样在屏幕上显示上下嵌套的TAB效果,单击后切换内容,在内容页上又重新显示二级的TAB,实现嵌套,如示例图所示,注意:对于TabHost、布局文件中必须包含: TabHost、TabWidget ...
Android应用源码开发Demo,主要用于毕业设计学习。
看到很多童鞋都在找关于android TabHost的菜单ui 自己在练习的时候写了一个,希望可以帮助你更喜爱android