Android UI TabHost总结
来源:互联网 发布:网络上py的是什么意思 编辑:程序博客网 时间:2024/05/16 10:16
TabHost 包括两部分,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文件:
为了让tabHost显示在下方,要将RadioGroup的layout_gravity设置为bottom,再将FrameLayout的layout_weight设置为1,这样就可以将RadioGroup撑到最下方。style="@style/main_tab_bottom"里面定义了样式文件。
接下来就是在activity中初始化并添加tabhost:
初始化每个RadioButton并为其添加setOnCheckedChangeListener事件,当点击相应的RadioButton时就可以通过setCurrentTabByTag()方法显示到当前页面。
小结:
1、在一个tabActivity里面嵌套一个tabAcitivity, 如果在子tabActivity里面显示AlertDialog、ProgressDialog的话,就会引发此错误:android.view.WindowManager$BadTokenException: Unable to add window
解决方法:
可以把创建dialog时传递的参数xxx.this改成this.getParent(),其中的xxx为Activity
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文件:
- <?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">
- <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="fill_parent"
- android:layout_weight="1.0"
- android:paddingBottom="53px"/>
- <TabWidget
- android:id="@android:id/tabs"
- android:layout_alignParentBottom="true"
- android:layout_width="fill_parent"
- android:layout_height="50px"
- android:visibility="gone"
- android:layout_weight="0.0"/>
- <RadioGroup
- android:gravity="center_vertical"
- android:orientation="horizontal"
- android:id="@+id/main_radio"
- android:background="@drawable/radiogroup_background"
- android:layout_width="fill_parent"
- android:layout_height="50dip"
- android:layout_gravity="bottom">
- <RadioButton
- android:id="@+id/main_index_button"
- android:layout_marginTop="1.0dip"
- android:layout_marginRight="5dip"
- android:text="@string/main_name"
- android:drawableTop="@drawable/unistall"
- style="@style/main_tab_bottom"
- android:background="@drawable/radio_bg"/>
- <RadioButton
- android:id="@+id/main_running_button"
- android:layout_marginTop="1.0dip"
- android:layout_marginRight="5dip"
- android:text="@string/run_manager_name"
- android:drawableTop="@drawable/unistall"
- style="@style/main_tab_bottom"
- android:background="@drawable/radio_bg"/>
- <RadioButton
- android:id="@+id/main_uninstall_button"
- android:layout_marginTop="1.0dip"
- android:text="@string/uninstall_manager_name"
- android:drawableTop="@drawable/unistall"
- style="@style/main_tab_bottom"
- android:background="@drawable/radio_bg"/>
- </RadioGroup>
- </LinearLayout>
- </TabHost>
<?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"><LinearLayoutandroid:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="fill_parent"android:layout_height="fill_parent" android:layout_weight="1.0"android:paddingBottom="53px"/><TabWidgetandroid:id="@android:id/tabs"android:layout_alignParentBottom="true"android:layout_width="fill_parent"android:layout_height="50px" android:visibility="gone"android:layout_weight="0.0"/><RadioGroupandroid:gravity="center_vertical"android:orientation="horizontal"android:id="@+id/main_radio"android:background="@drawable/radiogroup_background"android:layout_width="fill_parent"android:layout_height="50dip"android:layout_gravity="bottom"><RadioButtonandroid:id="@+id/main_index_button"android:layout_marginTop="1.0dip"android:layout_marginRight="5dip"android:text="@string/main_name"android:drawableTop="@drawable/unistall"style="@style/main_tab_bottom"android:background="@drawable/radio_bg"/><RadioButtonandroid:id="@+id/main_running_button"android:layout_marginTop="1.0dip"android:layout_marginRight="5dip"android:text="@string/run_manager_name"android:drawableTop="@drawable/unistall"style="@style/main_tab_bottom"android:background="@drawable/radio_bg"/><RadioButtonandroid:id="@+id/main_uninstall_button"android:layout_marginTop="1.0dip"android:text="@string/uninstall_manager_name"android:drawableTop="@drawable/unistall"style="@style/main_tab_bottom"android:background="@drawable/radio_bg"/></RadioGroup></LinearLayout></TabHost>
为了让tabHost显示在下方,要将RadioGroup的layout_gravity设置为bottom,再将FrameLayout的layout_weight设置为1,这样就可以将RadioGroup撑到最下方。style="@style/main_tab_bottom"里面定义了样式文件。
接下来就是在activity中初始化并添加tabhost:
- tabHost = (TabHost) findViewById(android.R.id.tabhost);
- tabHost.addTab(Constant.tabHost.newTabSpec("Main")
- .setIndicator(getString(R.string.main_name),null)
- .setContent(new Intent(this, Main.class)));
- tabHost.addTab(Constant.tabHost.newTabSpec("RunManager")
- .setIndicator(getString(R.string.run_manager_name),null)
- .setContent(new Intent(this, RunManager.class)));
- tabHost.addTab(Constant.tabHost.newTabSpec("UninstallManager")
- .setIndicator(getString(R.string.uninstall_manager_name),null)
- .setContent(new Intent(this, UninstallManager.class)));
tabHost = (TabHost) findViewById(android.R.id.tabhost);tabHost.addTab(Constant.tabHost.newTabSpec("Main").setIndicator(getString(R.string.main_name),null).setContent(new Intent(this, Main.class)));tabHost.addTab(Constant.tabHost.newTabSpec("RunManager").setIndicator(getString(R.string.run_manager_name),null).setContent(new Intent(this, RunManager.class)));tabHost.addTab(Constant.tabHost.newTabSpec("UninstallManager").setIndicator(getString(R.string.uninstall_manager_name),null).setContent(new Intent(this, UninstallManager.class)));
初始化每个RadioButton并为其添加setOnCheckedChangeListener事件,当点击相应的RadioButton时就可以通过setCurrentTabByTag()方法显示到当前页面。
- private void initRadios() {
- ((RadioButton) findViewById(R.id.main_index_button))
- .setOnCheckedChangeListener(this);
- ((RadioButton) findViewById(R.id.main_running_button))
- .setOnCheckedChangeListener(this);
- ((RadioButton) findViewById(R.id.main_uninstall_button))
- .setOnCheckedChangeListener(this);
- }
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- if(isChecked){
- switch (buttonView.getId()) {
- case R.id.main_index_button:
- tabHost.setCurrentTabByTag("Main");
- break;
- case R.id.main_running_button:
- tabHost.setCurrentTabByTag("RunManager");
- break;
- case R.id.main_uninstall_button:
- tabHost.setCurrentTabByTag("UninstallManager");
- break;
- }
- }
- }
private void initRadios() {((RadioButton) findViewById(R.id.main_index_button)).setOnCheckedChangeListener(this);((RadioButton) findViewById(R.id.main_running_button)).setOnCheckedChangeListener(this);((RadioButton) findViewById(R.id.main_uninstall_button)).setOnCheckedChangeListener(this);}@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if(isChecked){switch (buttonView.getId()) {case R.id.main_index_button:tabHost.setCurrentTabByTag("Main");break;case R.id.main_running_button:tabHost.setCurrentTabByTag("RunManager");break;case R.id.main_uninstall_button:tabHost.setCurrentTabByTag("UninstallManager");break;}}}
小结:
1、在一个tabActivity里面嵌套一个tabAcitivity, 如果在子tabActivity里面显示AlertDialog、ProgressDialog的话,就会引发此错误:android.view.WindowManager$BadTokenException: Unable to add window
解决方法:
可以把创建dialog时传递的参数xxx.this改成this.getParent(),其中的xxx为Activity
- Android UI TabHost总结
- Android UI, TabHost (过时)
- Android---UI篇---Tab Tabhost
- Android学习之 UI--Tabhost
- TabHost用法总结【Android】
- Android TabHost总结
- Android UI设计-Tabhost标签页
- Android UI学习之TabHost常用技巧
- Android UI之Tab(TabActivity+TabHost实现)
- android-----UI布局之TabHost与TextView
- Android TabHost,TabWidget,FrameLayout 选项卡总结
- Android TabHost,TabWidget选项卡总结
- Android TabHost,TabWidget选项卡总结
- Android tabhost
- Android:TabHost
- TabHost Android
- Android TabHost
- Android TabHost
- mobile DRAM
- gsm900 频带划分
- 后缀数组
- unittest模块
- 什么是机器智能
- Android UI TabHost总结
- protege 学习
- Nginx内核参数相关的优化设定
- 运算放大器选型
- Case ignore compare strings
- 【STL】sort函数的用法
- Android客户端与服务器端的json数据交互(很详细)
- POJ2769 Reduced ID Numbers
- 菜单项的消息 COMMAND和UPDATA_COMMAND_UI