android 选项卡TabHost

 选项卡主要有TabHost、TabWiget和 FramentLayout3个组件组成,用于实现一个多标签的用户界面,通过他可以将一个复杂的对话分隔成若干个标签页,实现对信息的分类显示和管理。使用给组件不仅可以使界面美观大方,还可以有效地减少窗体个数。

  在Android中,实现选项卡的一半步骤如下:

(1)在布局文件中添加实现选项卡所需的TabHost、TabWiget和 FramentLayout组件。

(2)编写各个标签页中要显示内容所对应的XML布局文件。

(3)在Activity个,获取并初始化TabHost组件。

(4)为TabHost对象添加标签页。


1.主布局文件:

activity_main.layout.xml:

[java]  view plain copy print ?
  1. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  
  3.     android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"  
  4.     android:paddingRight="@dimen/activity_horizontal_margin"  
  5.     android:paddingTop="@dimen/activity_vertical_margin"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"  
  7.     android:id="@android:id/tabhost">  
  8.   
  9.     <RelativeLayout  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="fill_parent"  
  12.         android:orientation="vertical">  
  13.         <TabWidget  
  14.             android:layout_width="fill_parent"  
  15.             android:layout_height="wrap_content"  
  16.             android:id="@android:id/tabs"  
  17.             android:layout_alignParentBottom="true"></TabWidget>  
  18.         <FrameLayout  
  19.             android:layout_width="fill_parent"  
  20.             android:layout_height="fill_parent"  
  21.             android:id="@android:id/tabcontent"></FrameLayout>  
  22.     </RelativeLayout>  
  23.   
  24. </TabHost>  
2.编写各标签页要显示内容对应的XML布局文件。

tab1.xml

[java]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent" android:layout_height="match_parent"  
  4.     android:orientation="vertical"  
  5.     android:id="@+id/linearLayout01">  
  6.   
  7.     <TextView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="我是Tab1"/>  
  11.     <TextView  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:background="@drawable/img01"/>  
  15. </LinearLayout>  
tab2.xml:

 
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent" android:layout_height="match_parent"  
  4.     android:orientation="vertical"  
  5.     android:id="@+id/linearLayout02">  
  6.   
  7.     <TextView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="我是Tab2"/>  
  11.     <TextView  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:background="@drawable/img02"/>  
  15. </LinearLayout>  
MainActivity.java

[java]  view plain copy print ?
  1. public class MainActivity extends ActionBarActivity {  
  2.   
  3.     private TabHost tabHost=null;  
  4.     private TabWidget tabWidget=null;  
  5.     private FrameLayout frameLayout=null;  
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_main);  
  10.   
  11.         tabHost= (TabHost) findViewById(android.R.id.tabhost); //声明TabHost组件对象  
  12.         tabHost.setup();  //初始化TabHost组件  
  13.   
  14.         //为TabHost对象添加标签页。  
  15.         LayoutInflater inflater=LayoutInflater.from(this); //申明LayoutInflater对象  
  16.         inflater.inflate(R.layout.tab1,tabHost.getTabContentView());  
  17.         inflater.inflate(R.layout.tab2,tabHost.getTabContentView());  
  18.         tabHost.addTab(tabHost.newTabSpec("未接电话")  
  19.                 .setIndicator("未接电话")  
  20.                 .setContent(R.id.linearLayout01)); //添加第一个标签页  
  21.         tabHost.addTab(tabHost.newTabSpec("已接电话")  
  22.                 .setIndicator("已接电话",getResources().getDrawable(R.drawable.icon))  
  23.                 .setContent(R.id.linearLayout02)); //添加第二个标签页  
  24.     }  
  25.   
  26.     @Override  
  27.     public boolean onCreateOptionsMenu(Menu menu) {  
  28.         // Inflate the menu; this adds items to the action bar if it is present.  
  29.         getMenuInflater().inflate(R.menu.menu_main, menu);  
  30.         return true;  
  31.     }  
  32.   
  33.     @Override  
  34.     public boolean onOptionsItemSelected(MenuItem item) {  
  35.         // Handle action bar item clicks here. The action bar will  
  36.         // automatically handle clicks on the Home/Up button, so long  
  37.         // as you specify a parent activity in AndroidManifest.xml.  
  38.         int id = item.getItemId();  
  39.   
  40.         //noinspection SimplifiableIfStatement  
  41.         if (id == R.id.action_settings) {  
  42.             return true;  
  43.         }  
  44.   
  45.         return super.onOptionsItemSelected(item);  
  46.     }  
  47. }  


setIndicator()可以设置选项卡得图标和文字。

需要注意几点是: 

                           TabHost必须为 android:id="@android:id/tabhost" ,

                           TabWidget 必须为 android:id="@android:id/tabs" ,

                                         FrameLayout android:id="@android:id/tabcontent" ;

效果图(更改TabWiget位置可以实现让TabWiget显示在顶端或低端):

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值