Android TAb分页菜单实现概括
发布时间:2021-12-14 21:41:29 所属栏目:教程 来源:互联网
导读:这里实现的是底部菜单: 布局文件:(我们通过RelativeLayout 可以把TabWidget定位在底部) ?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=fill
这里实现的是底部菜单: 布局文件:(我们通过RelativeLayout 可以把TabWidget定位在底部) <?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="fill_parent" android:layout_height="fill_parent" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="3dp" > <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" > </FrameLayout> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignBottom="@android:id/tabcontent" android:background="@drawable/tabbar_bg" /> </RelativeLayout> </TabHost> 在这里我们将说明一下:之前我是获取到TabWidget的view试图及内部icon和title,然后控制实现其效果,但是我们也可以用另外一种方式,也就是我们调用TabHost.TabSpec 的setIndicator(View view);这个方法,我们可以定制显示的view, 代码片段: /*** * 创建footerview */ public void createFooterView() { tabHost = getTabHost(); // The activity TabHost view = new TabView(this, R.drawable.tabbar_icon_home, R.drawable.tabbar_icon_home_selecotr); view.setBackgroundDrawable(this.getResources().getDrawable( R.drawable.footer_view_selector)); intent = new Intent(MainActivity.this, HomeActivity.class); spec = tabHost.newTabSpec("num1").setIndicator(view).setContent(intent); tabHost.addTab(spec); view = new TabView(this, R.drawable.tabbar_icon_search, R.drawable.tabbar_icon_search_selecotr); view.setBackgroundDrawable(this.getResources().getDrawable( R.drawable.footer_view_selector)); intent = new Intent(MainActivity.this, HomeActivity.class); spec = tabHost.newTabSpec("num2").setIndicator(view).setContent(intent); tabHost.addTab(spec); view = new TabView(this, R.drawable.tabbar_icon_cart, R.drawable.tabbar_icon_cart_selector); view.setBackgroundDrawable(this.getResources().getDrawable( R.drawable.footer_view_selector)); intent = new Intent(MainActivity.this, HomeActivity.class); spec = tabHost.newTabSpec("num3").setIndicator(view).setContent(intent); tabHost.addTab(spec); view = new TabView(this, R.drawable.tabbar_icon_more, R.drawable.tabbar_icon_more_selecotr); view.setBackgroundDrawable(this.getResources().getDrawable( R.drawable.footer_view_selector)); intent = new Intent(MainActivity.this, HomeActivity.class); spec = tabHost.newTabSpec("num4").setIndicator(view).setContent(intent); tabHost.addTab(spec); } /*** * 自定义view * */ class TabView extends LinearLayout { ImageView imageView; public TabView(Context c, int drawable, int drawableselec) { super(c); imageView = new ImageView(c); // 可以定制点击后状态 StateListDrawable listDrawable = new StateListDrawable(); // 未选 listDrawable.addState(SELECTED_STATE_SET, this.getResources() .getDrawable(drawableselec)); // 选择 listDrawable.addState(ENABLED_STATE_SET, this.getResources() .getDrawable(drawable)); imageView.setImageDrawable(listDrawable);// 引用 StateListDrawable setGravity(Gravity.CENTER); addView(imageView); } } 这样我们就实现想要的效果了.(建议使用这种方法,我的项目就是用的这个实现的.) 如果我是图标和文字分开的,我们也可以用(RadioButton代替,也许大家都不陌生,一会我简单介绍下) 这个源码是因为项目里面用的。 linux ![]() (编辑:PHP编程网 - 黄冈站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |