Android TabHost布局
16lz
2021-01-23
Android中使用TabHost 可以在一个屏幕间进行不同版面的切换:
在Android中要实现Tab切换页面的效果,大致有两种方法:一是利用layout下的xml配置来实现tab的布局,二是动态加载tabHost进行布局。
1、利用xml文件配置,引用xml的activity不用继承于TabActivity,通过findViewById方法获取TabHost,
具体的xml见下:
<?xml version="1.0" encoding="utf-8"?><TabHost android:id="@+id/tabhost" android:layout_width="fill_parent"android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"><LinearLayout android:orientation="vertical"android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- this is tabs content --><FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="wrap_content"android:paddingBottom="62px"><!-- tab1 content --><LinearLayout android:id="@+id/content1"android:layout_width="fill_parent" android:layout_height="fill_parent"android:orientation="vertical">。。。。。。。。。。。。</LinearLayout> <!-- tab2 content --><LinearLayout android:id="@+id/content2"android:layout_width="fill_parent" android:layout_height="fill_parent"android:orientation="vertical"> 。。。。。。。。。。。</LinearLayout></FrameLayout><RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <TabWidget android:id="@android:id/tabs" android:layout_alignParentBottom="true" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </RelativeLayout> </LinearLayout></TabHost>
xml中TabHost,FrameLayout,TabWidget 元素都要引用系统的id,若改用其他id会抛异常的,绿色的部分可以自定义
Activity部分代码:
setContentView(R.layout.***); //设置上面的xml文件
TabHost tabhost = (TabHost) findViewById(R.id.tabhost);tabhost.setup(); // 初始化TabHost容器// tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1", null).setContent(R.id.content1));TabHost.TabSpec tab_1 = tabhost.newTabSpec("tab_1"); // 设置一个tabtab_1.setContent(R.id.content1); // 设置tab里面的布局tab_1.setIndicator("按地名查询"); // 设置tab的标题和图表tabhost.addTab(tab_1); // 在tabhost上添加新的tab// tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab2", null).setContent(R.id.content2));TabHost.TabSpec tab_2 = tabhost.newTabSpec("tab_2");tab_2.setContent(R.id.content2);tab_2.setIndicator("按起点终点查询");tabhost.addTab(tab_2);// 设置默认显示第几个tabtabhost.setCurrentTab(0);
2、继承TabActivity 类,,Activity中使用getTabHost()获得TabHost,使用android的自己内部定义好的.xml资源文件作容器文件,进行动态布局;
tab_search.xml,用来布局tabHost下的content
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tab_hs_id"> <TextView android:id="@+id/tab_search_tv" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </FrameLayout>
Activity中的代码:
tabHost = getTabHost();
LayoutInflater inflater_tab3 = LayoutInflater.from(this);inflater_tab3.inflate(R.layout.tab_search, tabHost.getTabContentView()); /* * R.layout.tab_search已被LayoutInflater注册,所以这个content可以通过findViewById获得其对象 */TabHost.TabSpec tabSpec_near = tabHost.newTabSpec("nearest");tabSpec_near.setIndicator("Nearest", null);tabSpec_near.setContent(R.id.tab_hs_id); // 绑定一个新的LayouttabHost.addTab(tabSpec_near); //添加一个tab setContentView(tabHost); //设置avtivity的布局为tabHost
更多相关文章
- android 五大布局文件
- android根据uri或文件绝对路径获取文件基本信息
- RelativieLayout布局中的android:gravity="center"属性
- Android Layout.xml布局属性
- android 虚拟键盘遮挡布局 顶起布局
- Android 打开本地pdf文件,android 加载pdf文件
- LinearLayout 线性布局管理器