这两天上论坛,发现有几个人问类似的问题,想想有空整理一个demo发上来。。。
现在正处在前个项目正要走,后一个项目赶着来的那么一点点空隙中。。。
这个是我项目中遇到的问题,也在论坛中发帖求助过,虽然没有得到完整的答案吧,不过还是有很多朋友提供思路了。在这里谢谢帮助过我的兄弟姐妹们了。
言归正传!
在iphone开发中貌似有个UITabBarController,(我以为是toolbar,四楼的兄弟更正的),UITabBarController在底部,也有对应的切换效果,都封装好了。但是在android的中,这个东西它在顶部。。。我也不明白为什么这么设计,标新立异?我觉得在底部方便很多,我们的设计也是这样设计的,所以我也只有改咯。
个人认为设计不太好的tabhost,单手拿手机不好操作,不过下面有个兄弟提醒因为有menu的存在,呵呵,我光想不方便了。

整体的思想就是不进行startactivity,而是通过广播发送数据,发送之前切换到对应的tab。从而避免很多startactivity出现的麻烦,比如tabwidget不见了,跳转黑屏等等。。。
弹出窗可以获取到tabwidget对应的标签位置,设置其监听事件,阻塞掉原本tabwidget的切换就可以了。
先把这些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">
- <LinearLayout android:orientation="vertical"
- android:layout_width="fill_parent" android:layout_height="fill_parent">
- <FrameLayout android:id="@android:id/tabcontent"
- android:layout_width="fill_parent" android:layout_height="0dip"
- android:layout_weight="1" />
- <TabWidget android:id="@android:id/tabs"
- android:layout_width="fill_parent" android:layout_height="wrap_content"
- />
- </LinearLayout>
- </TabHost>
复制代码 注意每个控件的相对位置,位置对了,那个鬼东西才能跑底部去,自定义的tabhost就是如此了。。。

然后上代码。。。
public class TabManager extends TabActivity
你这边继承了Tabactivity, 同时布局文件中又用了android:id="@android:id/tabhost" ,所以你没错。你可以改下这个ID试试,一改就错了。
如果想要自定义这个ID,就不能继承TabActivity。
我也没试了,大家有空去试试。。。- Constant.tabHost = (TabHost) findViewById(android.R.id.tabhost);
- LayoutInflater.from(this).inflate(R.layout.tabcontent,
- Constant.tabHost.getTabContentView(), true);
- tabWidget = Constant.tabHost.getTabWidget();
- Constant.tabHost.addTab(Constant.tabHost.newTabSpec("tab1")
- .setIndicator("Tab1",th.getResources().getDrawable(R.drawable.ic_menu_home_tab)).setContent(new Intent(this, Tab1.class)));
复制代码- View v = tabWidget.getChildAt(i);
- // 设置tab背景颜色 外层有个循环,每个tab都要设置一次
- v.setBackgroundResource(R.drawable.tab_indicator);
- tab_indicator文件:
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <!-- Non focused states -->
- <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@color/tab_unselected" />
- <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@color/tab_selected" />
- <!-- Focused states -->
- <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@color/tab_focus" />
- <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@color/tab_focus" />
- <!-- Pressed -->
- <item android:state_pressed="true" android:drawable="@color/tab_press"/>
- </selector>
复制代码 下面是弹出窗口的- View view = tabWidget.getChildAt(4);
- view.setOnClickListener(new OnClickListener() {
复制代码 获取到你要弹出窗口的标签,设置setOnClickListener事件,就可以阻塞掉tabhost原本的事件。
Constant.tabHost = (TabHost) findViewById(android.R.id.tabhost);
Constant.tabHost.setCurrentTab(0);跳转就可以是这样做了。
Constant.tabHost 是全局变量,这个是关键,为了切换tab不会出现黑屏跳转效果。- txTextView.setText("点击切换到tab3");
- txTextView.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Constant.tabHost.setCurrentTab(2);//这里是全局变量的tabhost
- Intent intent = new Intent("com.niushu.recbroad");
- intent.putExtra("id","从tab1过来的牛叔");
- Tab1.this.sendBroadcast(intent);
- }
- });
复制代码 这个是在tab1中点击文字,首先切换到对应的tab,Constant.tabHost.setCurrentTab(2);,启动对应的activity,然后发送广播。用这个方法Constant.tabHost.setCurrentTab切换,就没有那些乱七八糟的麻烦。
如果发送广播过去之后,接下来要用线程操作的话,最好在建一个标志位,全局变量的。避免一些线程跟广播资源之间的冲突。
下面是接受广播的代码:需要注意在生命周期方法中注册和注销广播接收器- public class Broad extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- disid_Broad = intent.getStringExtra("id");
- Toast.makeText(Tab3.this, disid_Broad, 1).show();
- Log.i("Broad", "onReceive");
- }
- }
复制代码 |
|