关于自定义TabHost的使用和注意事项
来源:互联网 发布:php 简单工厂模式 编辑:程序博客网 时间:2024/05/29 10:59
最近搞个毕业设计,想搞个底部菜单的效果,可是网上找了很多代码,各有不同,感觉很乱,于是想写篇博客总结一下,方便日后查询.\
第一次方法:使用tabhost+ActivityGroup实现菜单及分页效果
首先,是主xml布局-->main.xml
代码如下:
<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <RelativeLayout android:id="@+id/linearLayout1" 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" android:layout_alignParentBottom="true" > </TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" > </FrameLayout> </RelativeLayout></TabHost>
从代码可知,要实现tabhost必须要有三个部分:TabHost,TabWidget,Framelayout.
1.最外层的TabHost可以自定义id也可以使用系统id@android:id/tabhost,我这使用的是自定义id,其中的区别后面会有说明,只要是Tabhost函数的获取方式不同.
2,tabwidget 是存放你的菜单的参数.,id必须为系统id @android:id/tabs
3.FrameLayout 是存放你的分页的参数,id必须为系统id @android:id/tabcontent ,
然后下面各个分页的xml (这里我们弄三个分页)
tab1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是tab1" /></LinearLayout>
tab2.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是tab2" /></LinearLayout>tab3.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是tab3" /></LinearLayout>
布局都非常简单.只有一个textview,然后显示不同文字;
接下来是菜单的布局文件,tabmini.xml (布局也很简单)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@color/grgray" android:gravity="center_horizontal" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/kaoqin" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
然后是主程序代码了
public class M1 extends ActivityGroup implements OnTabChangeListener {private TabHost host;private ViewFlipper flipper = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.tab1);host = (TabHost) findViewById(R.id.tabhost); // 上面提到过,由于我们用的是自定义的tabhost的id,所以用FindViewById找到TabHost.// 但是如果你用的是系统的id,就必须该activity继承TabActivity,然后通过getTabHost()方法找到TabHosthost.setup(); // 该方法是为了初始化我们的TabHost的几个参数,上面有将host.setup(this.getLocalActivityManager());host.setCurrentTab(1); // 默认为分页1Intent intent1 = new Intent(this, BodyActivity.class);Intent intent2 = new Intent(this, Information.class);Intent intent3 = new Intent(this, ModeActivity.class);host.addTab(host.newTabSpec("tab1").setIndicator(createview(R.drawable.seek, "扫描签到")).setContent(intent1)); // 将一下括号内的三个参数,第一个是设置标签也得名字,只要不重复就行,第二个是设置菜单的样式,我们这里是一个图片加文字的样式,,第三个是设置菜单选中时要显示的activity// !!!!!!需要注意的是,网上有人第三个参数传递的是id,这样的后果是虽然能正常显示各个分页,但每个分页没有功能,应为只是纯粹加载视图, 要让每个页面有功能,必须传intent!!!host.addTab(host.newTabSpec("tab3").setIndicator(createview(R.drawable.tab_myzone_opacity, "个人信息")).setContent(intent2));host.addTab(host.newTabSpec("tab4").setIndicator(createview(R.drawable.mode, "签到生成")).setContent(intent3));host.setOnTabChangedListener(this);}public View createview(int id, String str) {View view1 = LayoutInflater.from(this).inflate(R.layout.tab_mini, null); // 这里是加载我们菜单布局,并且给菜单赋值ImageView imageView1 = (ImageView) view1.findViewById(R.id.imageView1);TextView textView1 = (TextView) view1.findViewById(R.id.textView1);imageView1.setImageResource(id);textView1.setText(str);return view1;}/** * 判断分页是否被选中 * * @param tabHost */public void updateTab(TabHost tabHost) {for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {View view = tabHost.getTabWidget().getChildAt(i);// ImageView imageView = (ImageView) view// .findViewById(R.id.imageView1);// TextView textView = (TextView) view.findViewById(R.id.textView1);if (tabHost.getCurrentTab() == i) {view.setBackgroundResource(R.color.hs); // 选中时,该部分菜单,背景颜色改变} else {view.setBackgroundResource(R.color.grgray);}}}
详细说明代码中都有了,记得上面的activity继承的是ActivityGroup 而不是activity,因为我们在此activity中装载不同的activity分页
最后还是附上效果图吧
声明:以上纯手写,有错勿喷,自行参考
0 0
- 关于自定义TabHost的使用和注意事项
- 关于TabHost的用法以及注意事项
- TabHost的一点注意事项
- 关于Cookie的使用案例和注意事项
- 关于apk 反编译的使用和注意事项
- Android中关于TabHost的使用简介
- 使用自定义RadioButton和ViewPager实现TabHost效果和带滑动的页卡效果。
- Andriod TabHost的使用TabHost
- TabHost的自定义
- android Tabhost部件(三)--使用自定义view和fragement实现类似TabHost样式
- 使用TabHost点击第一次无法获得数据的问题,自定义TabHost。
- 关于 TabHost 的学习
- TabHost注意事项
- 关于重载的概念和使用时的注意事项
- 关于COMMIT和ROLLBACK语句的使用注意事项
- [android]关于SharedPreferences putString和commit的使用注意事项
- 关于Block的使用和5点注意事项
- iOS开发:关于automaticallyAdjustsScrollViewInsets的使用和注意事项
- linux下安装nodejs
- 对html的作用
- C# WinForm - WebBrowser
- Snmp学习总结(一)——Snmp的基本概念
- (1.2.1.4)双向循环链表
- 关于自定义TabHost的使用和注意事项
- html5&css3 Learning
- 由scrollView的缩放,发现的关于2种触摸模式的问题
- STM32学习日记4.BKP
- 后台开发面试
- 单反回收租赁销售
- Populating Next Right Pointers in Each Node
- Java接口与类的关系理解
- 八、oc中类的一些基本知识