鲁迅全集mobi 百度云:android TabHost小结

来源:百度文库 编辑:九乡新闻网 时间:2024/05/06 11:38:43

android TabHost小结

    博客分类:
  • UI相关
AndroidXML.netBlogTabHost是整个Tab的容器,包括两部分,TabWidget和FrameLayout。TabWidget就是每个tab的标签,FrameLayout则是tab内容。

1、如果我们使用extends TabAcitivty,如同ListActivity,TabHost必须设置为@android:id/tabhost
2、TabWidget必须设置android:id为@android:id/tabs
3、FrameLayout需要设置android:id为@android:id/tabcontent
4、参考这儿:http://blog.csdn.net/flowingflying/archive/2011/04/06/6304289.aspx

先自定义一个xml文件:
Java代码  
  1.   
  2.     android:id="@android:id/tabhost"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">   
  5.     
  6.         android:orientation="vertical"  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="fill_parent">   
  9.     
  10.         android:id="@android:id/tabcontent"  
  11.         android:layout_width="fill_parent"  
  12.         android:layout_height="fill_parent"  
  13.          android:layout_weight="1.0"  
  14.         android:paddingBottom="53px"/>   
  15.     
  16.         android:id="@android:id/tabs"  
  17.         android:layout_alignParentBottom="true"  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="50px"    
  20.         android:visibility="gone"  
  21.         android:layout_weight="0.0"/>   
  22.         
  23.             android:gravity="center_vertical"  
  24.             android:orientation="horizontal"  
  25.             android:id="@+id/main_radio"  
  26.             android:background="@drawable/radiogroup_background"  
  27.             android:layout_width="fill_parent"  
  28.             android:layout_height="50dip"  
  29.             android:layout_gravity="bottom">   
  30.             
  31.                 android:id="@+id/main_index_button"  
  32.                 android:layout_marginTop="1.0dip"  
  33.                 android:layout_marginRight="5dip"  
  34.                 android:text="@string/main_name"  
  35.                 android:drawableTop="@drawable/unistall"  
  36.                 style="@style/main_tab_bottom"  
  37.                 android:background="@drawable/radio_bg"/>   
  38.             
  39.                 android:id="@+id/main_running_button"  
  40.                 android:layout_marginTop="1.0dip"  
  41.                 android:layout_marginRight="5dip"  
  42.                 android:text="@string/run_manager_name"  
  43.                 android:drawableTop="@drawable/unistall"  
  44.                 style="@style/main_tab_bottom"  
  45.                 android:background="@drawable/radio_bg"/>   
  46.             
  47.                 android:id="@+id/main_uninstall_button"  
  48.                 android:layout_marginTop="1.0dip"  
  49.                 android:text="@string/uninstall_manager_name"  
  50.                 android:drawableTop="@drawable/unistall"  
  51.                 style="@style/main_tab_bottom"  
  52.                 android:background="@drawable/radio_bg"/>   
  53.            
  54.        
  55.   

为了让tabHost显示在下方,要将RadioGroup的layout_gravity设置为bottom,再将FrameLayout的layout_weight设置为1,这样就可以将RadioGroup撑到最下方。style="@style/main_tab_bottom"里面定义了样式文件。

接下来就是在activity中初始化并添加tabhost:
Java代码  
  1. tabHost = (TabHost) findViewById(android.R.id.tabhost);   
  2.         tabHost.addTab(Constant.tabHost.newTabSpec("Main")   
  3.                 .setIndicator(getString(R.string.main_name),null)   
  4.                 .setContent(new Intent(this, Main.class)));   
  5.         tabHost.addTab(Constant.tabHost.newTabSpec("RunManager")   
  6.                 .setIndicator(getString(R.string.run_manager_name),null)   
  7.                 .setContent(new Intent(this, RunManager.class)));   
  8.         tabHost.addTab(Constant.tabHost.newTabSpec("UninstallManager")   
  9.                 .setIndicator(getString(R.string.uninstall_manager_name),null)   
  10.                 .setContent(new Intent(this, UninstallManager.class)));  


初始化每个RadioButton并为其添加setOnCheckedChangeListener事件,当点击相应的RadioButton时就可以通过setCurrentTabByTag()方法显示到当前页面。
Java代码  
  1. private void initRadios() {   
  2.         ((RadioButton) findViewById(R.id.main_index_button))   
  3.                 .setOnCheckedChangeListener(this);   
  4.         ((RadioButton) findViewById(R.id.main_running_button))   
  5.                 .setOnCheckedChangeListener(this);   
  6.         ((RadioButton) findViewById(R.id.main_uninstall_button))   
  7.                 .setOnCheckedChangeListener(this);   
  8.     }   
  9.        
  10.     @Override  
  11.     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {   
  12.         if(isChecked){   
  13.             switch (buttonView.getId()) {   
  14.             case R.id.main_index_button:   
  15.                 tabHost.setCurrentTabByTag("Main");   
  16.                 break;   
  17.             case R.id.main_running_button:   
  18.                 tabHost.setCurrentTabByTag("RunManager");   
  19.                 break;   
  20.             case R.id.main_uninstall_button:   
  21.                 tabHost.setCurrentTabByTag("UninstallManager");   
  22.                 break;   
  23.             }   
  24.         }   
  25.     }  


小结:
1、在一个tabActivity里面嵌套一个tabAcitivity, 如果在子tabActivity里面显示AlertDialog、ProgressDialog的话,就会引发此错误:android.view.WindowManager$BadTokenException: Unable to add window

解决方法:
可以把创建dialog时传递的参数xxx.this改成this.getParent(),其中的xxx为Activity