Android之TabHost 的选项卡布局

TabHost的实现有两种方式:
第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost。各个Tab中的内容在布局文件中定义就行了。

 第二种方式,不继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的id必须是@android:id/tabs,FrameLayout的id必须是@android:id/tabcontent。

第一种方式:

1. 布局文件  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<? xml version = "1.0" encoding = "utf-8" ?>
<!-- Copyright 2009 Joe LaPenna -->
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
     android:layout_width = "fill_parent"
     android:layout_height = "fill_parent"
     android:background = "@drawable/home_gradient"
     android:orientation = "vertical" >
  
     < TabHost
         android:id = "@android:id/tabhost"
         android:layout_width = "fill_parent"
         android:layout_height = "fill_parent" >
  
         <!-- A paddingTop of zero will remove the strip below the tabs. -->
  
         < FrameLayout
             android:id = "@android:id/tabcontent"
             android:layout_width = "fill_parent"
             android:layout_height = "fill_parent"
             android:paddingBottom = "60dip" />
  
         < RelativeLayout
             android:layout_width = "fill_parent"
             android:layout_height = "fill_parent" >
<!--  android:background="#363636" -->
             < TabWidget
                 android:background = "@drawable/title_bg_night"
                 
                 android:id = "@android:id/tabs"
                 android:layout_width = "fill_parent"
                 android:layout_height = "60dip"
                 android:layout_alignParentBottom = "true"
                 android:tabStripEnabled = "false" />
         </ RelativeLayout >
     </ TabHost >
  
</ LinearLayout >

 

 2. 继承 TabActivity

1
2
3
public class MainTabActivity extends TabActivity {
     private TabHost mTabHost;
     private LayoutInflater inflater;

 

 inflater = LayoutInflater.from(this);   从这个TabActivity获取LayoutInflater

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
@Override
     protected void onCreate(Bundle arg0) {
         super .onCreate(arg0);
  
         requestWindowFeature(Window.FEATURE_NO_TITLE);
         setContentView(R.layout.main_tab);
  
         inflater = LayoutInflater.from( this );
  
         mTabHost = (TabHost) findViewById(android.R.id.tabhost);
  
         mTabHost.addTab(getTabSpec( "mydouban" , MeActivity. class , "我的豆瓣" ,
                 R.drawable.tab_main_nav_me));
  
         mTabHost.addTab(getTabSpec( "newbook" , NewBooksActivity. class , "新书" ,
                 R.drawable.tab_main_nav_book));
         mTabHost.addTab(getTabSpec( "mydouban" , MeActivity. class , "书评" ,
                 R.drawable.detail_comment_selected));
         mTabHost.addTab(getTabSpec( "mydouban" , SearchActivity. class , "搜索" ,
                 R.drawable.tab_main_nav_search));
         mTabHost.addTab(getTabSpec( "mydouban" , AboutActivity. class , "关于" ,
                 R.drawable.tab_main_nav_more));
  
     }
  
     private TabSpec getTabSpec(String tag, Class clazz, String name, int drawid) {
  
         TabSpec spec = mTabHost.newTabSpec(tag);
         // 指定显示内容
         Intent intent = new Intent( this , clazz);
         spec.setContent(intent);
         // 设置标签的样式
         spec.setIndicator(getIndicatorView(name, drawid));
         return spec;
     }
  
     // 获取view对象
     private View getIndicatorView(String name, int drawid) {
         View view = inflater.inflate(R.layout.tab_main_nav, null );
         ImageView ivicon = (ImageView) view.findViewById(R.id.ivIcon);
         TextView tvtitle = (TextView) view.findViewById(R.id.tvTitle);
         ivicon.setImageResource(drawid);
         tvtitle.setText(name);
         return view;
     }

3.每个选项卡的布局文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
     android:id = "@+id/tabMainNav"
     android:layout_width = "fill_parent"
     android:layout_height = "fill_parent"
     android:background = "@drawable/tab_main_nav_selector"
     android:gravity = "center_horizontal"
     android:orientation = "vertical" >
  
     < ImageView
         android:id = "@+id/ivIcon"
         android:layout_width = "30dip"
         android:layout_height = "30dip"
         android:layout_marginBottom = "1dip"
         android:layout_marginTop = "5dip"
         android:scaleType = "fitXY" />
  
     < TextView
         android:id = "@+id/tvTitle"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:layout_marginBottom = "5dip"
         android:textAppearance = "@style/TextViewStyleTabMainNav"
         android:textSize = "14dip" />
  
</ LinearLayout >

 

第二 种方式:

 

不继承TabActivity

     继承普通Activity,<TabWidget>标签id必须为tabs、<FrameLayout>标签id必须为tabcontent.这个方式在通过findViewById获得TabHost之后,必须要调用setup方法。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<? xml version = "1.0" encoding = "utf-8" ?>  
   <!-- 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" />  
             <!-- 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 >

// 如果没有继承TabActivity时,通过该种方法加载启动tabHost  
        tabHost.setup();  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class TabHostTest 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));  
     }  
}

 

 

 





转载于:https://www.cnblogs.com/feelbest/p/3696185.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值