TabActivity中子Activity相互跳转,及某个Tab需弹出窗的解决方案
在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,否則不能自定義tabhost,但在這就是這樣做了,也沒錯。還是自己實踐比較好,別盲目轉帖。 有人提醒的,多謝:樓主誤解了,其實網上說的是不能繼承tabactivity,否則不能自定義tabhost名。 你這邊繼承了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() {@Overridepublic 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 {@Overridepublic void onReceive(Context context, Intent intent) {disid_Broad = intent.getStringExtra("id");Toast.makeText(Tab3.this, disid_Broad, 1).show();Log.i("Broad", "onReceive");}}?
?
?
轉載于:https://www.cnblogs.com/vus520/archive/2012/03/22/2561972.html
總結
以上是生活随笔為你收集整理的TabActivity中子Activity相互跳转,及某个Tab需弹出窗的解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: UVA 10404 - Bachet's
- 下一篇: hdu 1892【二维树状数组】
