详解Android应用中使用TabHost组件进行布局的基本方法

所属分类: 软件编程 / Android 阅读数: 1454
收藏 0 赞 0 分享

TabHost布局文件

我们先来了解一下布局文件的基本内容:
1. 根标签及id

设置Android自带id : XML布局文件中, 可以使用 标签设置, 其中的id 需要引用 android的自带id :

android:id=@android:id/tabhost ;

getHost()获取前提 : 设置了该id之后, 在Activity界面可以使用 getHost(), 获取这个TabHost 视图对象;

示例 :

复制代码 代码如下:

<tabhost android:id="@android:id/tabhost" android:layout_height="match_parent" android:layout_width="match_parent"></tabhost>

2. TabWidget组件

选项卡切换 : 该组件是选项卡切换按钮, 通过点击该组件可以切换选项卡;

设置android自带id : 这个组件的id要设置成android的自带id : android:id=@android:id/tabs ;

TabHost必备组件 : 该组件与FrameLayout组件是TabHost组件中必备的两个组件;

切换按钮下方显示 : 如果想要将按钮放到下面, 可以将该组件定义在下面, 但是注意,FrameLayout要设置android:layout_widget = 1;

设置TabWidget大小 : 如果想要设置该按钮组件的大小, 可以设置该组件与FrameLayout组件的权重;

示例 :

复制代码 代码如下:

<tabwidget android:id="@android:id/tabs" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="horizontal/"></tabwidget>

3. FrameLayout组件

组件作用 : 该组件中定义的子组件是TabHost中每个页面显示的选项卡, 可以将TabHost选项卡显示的视图定义在其中;

设置android自带id : 这个组件的id要设置成android的自带的id : android:id=@android:id/tabcontent ;

示例 :

复制代码 代码如下:

<framelayout android:id="@android:id/tabcontent" android:layout_height="fill_parent" android:layout_weight="1" android:layout_width="fill_parent"></framelayout>

示例

2016412154201842.gif (354×175)

上图为最终效果图
代码结构图

2016412154240467.gif (221×432)

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@+id/hometabs" 
 android:orientation="vertical" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent"> 
 <!-- TabHost必须包含一个 TabWidget和一个FrameLayout--> 
 <TabHost android:id="@+id/tabhost" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  > 
  <LinearLayout 
   android:orientation="vertical" 
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent"> 
   <!-- TabWidget的id属性必须为 @android:id/tabs-->    
   <TabWidget android:id="@android:id/tabs" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
   </TabWidget> 
   <!-- FrameLayout的id属性必须为 @android:id/tabcontent--> 
    <FrameLayout android:id="@android:id/tabcontent" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <TextView android:id="@+id/view1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"/> 
     <TextView android:id="@+id/view2" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"/> 
     <TextView android:id="@+id/view3" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"/> 
    </FrameLayout> 
   
   </LinearLayout> 
 </TabHost> 
</LinearLayout> 

java代码如下

package cn.com.tagHost.test; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TabHost; 
import android.widget.TabWidget; 
 
public class TagHostTest2 extends Activity { 
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.main); 
  // 获取TabHost对象 
  TabHost tabHost = (TabHost) findViewById(R.id.tabhost); 
  // 如果没有继承TabActivity时,通过该种方法加载启动tabHost 
  tabHost.setup(); 
  tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("第一个标签", 
    getResources().getDrawable(R.drawable.icon)).setContent( 
    R.id.view1)); 
 
  tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("第三个标签") 
    .setContent(R.id.view3)); 
 
  tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("第二个标签") 
    .setContent(R.id.view2)); 
 } 
} 

运行得到正确的结果。
废话连篇:这里需要注意的是
第一:布局文件的格式。以及TabWidget和FrameLayout的id属性值。
第二:TabWidget代表的是标签部分,FrameLayout代表的点击标签后看到的内容部分。FrameLayout里面声明的组件意为具备成为标签内容的资格,具体的还要在代码中具体指定。
你是否也想要这种结果呢。让标签在下部分显示

2016412154317508.gif (362×516)

那么你只需要给main.xml进行下布局修改就可以了。

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@+id/hometabs" android:orientation="vertical" 
 android:layout_width="fill_parent" android:layout_height="fill_parent"> 
 <!-- TabHost必须包含一个 TabWidget和一个FrameLayout--> 
 <TabHost android:id="@+id/tabhost" android:layout_width="fill_parent" 
  android:layout_height="wrap_content"> 
  <LinearLayout android:orientation="vertical" 
   android:layout_width="fill_parent" android:layout_height="fill_parent"> 
 
   <!-- FrameLayout的id属性必须为 @android:id/tabcontent--> 
   <FrameLayout android:id="@android:id/tabcontent" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 
    <TextView android:id="@+id/view1" android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:text="hello baby!" 
     /> 
    <TextView android:id="@+id/view2" android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 
    <TextView android:id="@+id/view3" android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 
   </FrameLayout> 
   <RelativeLayout android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
 
    <!-- TabWidget的id属性必须为 @android:id/tabs--> 
    <TabWidget android:id="@android:id/tabs" 
     android:orientation="horizontal" android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:paddingBottom="0dp" 
     > 
    </TabWidget> 
   </RelativeLayout> 
  </LinearLayout> 
 </TabHost> 
</LinearLayout> 

为了让标签和父容器底部持平,我们使用了android:layout_alignParentBottom="true",该属性只有在RelativeLayout布局中才会存在哦、这也是为什么我们将tabWidget放入一个RelativeLayout中的原因。
此外,在lineaerLayout布局中,TabWidget和FrameLayout的位置可是调换了哦。

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多