Android 位于底部的Tab

来源:互联网 发布:爱国者淘宝店铺名字 编辑:程序博客网 时间:2024/05/01 05:01
 

第一步:首先你得了解TabHost和TabWidget, 将TabHost分为两个部分,一个是放内容的,还有一个就是放选项卡的,我们这里选项卡用TabWidget。TabHost的id应该写为

android:id="@android:id/tabhost",而不是以前的那个加号,这样可以直接在Activity里面通过getTabHost方法得到此TabHost对象。那如何才能将选项卡放在最底部呢,这其实很简单,还记得 RelativeLayout中android:layout_alignBottom 这个不,也就是说将TabWidget和选项卡内容放到RelativeLayout布局里,废话不多说了,看代码

     <?xml version="1.0" encoding="utf-8"?>  
  1. <TabHost android:layout_width="fill_parent"  
  2.     android:layout_height="fill_parent"  
  3.     android:id="@android:id/tabhost"  
  4.     xmlns:android="http://schemas.android.com/apk/res/android"  
  5.     >  
  6.     <RelativeLayout  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="fill_parent"  
  9.         android:orientation="vertical"  
  10.         android:padding="3dp"  
  11.     >  
  12.          <FrameLayout  
  13.              android:layout_width="fill_parent"  
  14.             android:layout_height="fill_parent"  
  15.             android:id="@android:id/tabcontent"  
  16.             android:layout_weight="1"   
  17.              >  
  18.          </FrameLayout>  
  19.           <TabWidget  
  20.             android:layout_width="fill_parent"  
  21.             android:layout_height="50dip"  
  22.             android:id="@android:id/tabs"  
  23.             android:layout_alignBottom = "@android:id/tabcontent"  
  24.             />  
  25.     </RelativeLayout>  
  26. </TabHost>  


以上xml代码可以通用哈,FrameLayout里用来放内容的,Tab的xml基本结构就这样搭好了,

第二步:接下来就是往Activity里面写点东东了,这个Activity必须要继承下TabActivity,要不然就算你setContentView了上面那个xml也没法通过getTabHost()获得布局中的tabHost。

然后通过getTabHost得到TabHost对象。我这里打算建立四个选项卡,newTabSpec 这是为你的每一个选项卡上面打上一个Tag,也就是标记标记而已,特殊情况下你可以通过这个来查找到你的选项卡。setIndicator就是在你的选项卡上设置一些内容,至于什么内容,你懂的,要么是文字,要么是图片,要么就是文字加图片,当然有时候复杂的话还会写上布局文件。我这里就放上一个图片吧,然后这个图片加上一些特效,懒得在xml写什么布局了,直接在代码写了,返回的是一个View对象,废话有点多,直接看代码吧

  private  class TabView extends LinearLayout {  
  1.     ImageView imageView ;  
  2. public TabView(Context c, int drawable, int drawableselec) {  
  3.     super(c);  
  4.     imageView = new ImageView(c);  
  5.     StateListDrawable listDrawable = new StateListDrawable();  
  6.     listDrawable.addState(SELECTED_STATE_SET, this.getResources()  
  7.             .getDrawable(drawableselec));  
  8.     listDrawable.addState(ENABLED_STATE_SET, this.getResources()  
  9.             .getDrawable(drawable));  
  10.     imageView.setImageDrawable(listDrawable);  
  11.     imageView.setBackgroundColor(Color.TRANSPARENT);  
  12.     setGravity(Gravity.CENTER);  
  13.     addView(imageView);  
  14. }  


上面的代码也就是自定义的一个View吧。

下面这个就是构建选项卡以及内容(选中的是第二个选项卡),我这里就把每个不同的选项卡内容分别放在不同的Activity里面,然后通过Tab来将这些Activity合并在一起,这样显得比较有层次感。

        @Override  
  1.     public void onCreate(Bundle savedInstanceState) {  
  2.         super.onCreate(savedInstanceState);  
  3.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  4.         setContentView(R.layout.main_tab);  
  5.         TabHost tabHost=getTabHost();  
  6.           
  7.         TabView view = null;  
  8.           
  9.         // 最近联系人   
  10.         view = new TabView(this, R.drawable.bg_tab_dial_normal, R.drawable.bg_tab_dial_normal);  
  11.         view.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.selecttabbackground));  
  12.               
  13.         TabSpec recentContactSpec=tabHost.newTabSpec("RecentContact");  
  14.         recentContactSpec.setIndicator(view);  
  15.         Intent recentContactIntent = new Intent(this, RecentContactActivity.class);  
  16.         recentContactSpec.setContent(recentContactIntent);  
  17.         // 联系人   
  18.         view = new TabView(this, R.drawable.bg_tab_contact_normal, R.drawable.bg_tab_contact_normal);  
  19.         view.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.selecttabbackground));  
  20.           
  21.         TabSpec contactBookSpec=tabHost.newTabSpec("ContactBook");  
  22.         contactBookSpec.setIndicator(view);  
  23.         Intent contactBookIntent = new Intent(this,ContactBookActivity.class);  
  24.         contactBookSpec.setContent(contactBookIntent);  
  25.           
  26.         // 短信   
  27.         view = new TabView(this, R.drawable.bg_tab_sms_normal, R.drawable.bg_tab_sms_normal);  
  28.         view.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.selecttabbackground));  
  29.           
  30.         TabSpec smsMessageSpec = tabHost.newTabSpec("SmsMessage");  
  31.         smsMessageSpec.setIndicator(view);  
  32.         Intent smsMessageIntent = new Intent(this, SmsMessageActivity.class);  
  33.         smsMessageSpec.setContent(smsMessageIntent);  
  34.           
  35.         //设置    
  36.         view = new TabView(this, R.drawable.bg_tab_setting_normal, R.drawable.bg_tab_setting_normal);  
  37.         view.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.selecttabbackground));  
  38.           
  39.         TabSpec settingSpec = tabHost.newTabSpec("Setting");  
  40.         settingSpec.setIndicator(view);  
  41.         Intent settingIntent = new Intent(this, SettingActivity.class);  
  42.         settingSpec.setContent(settingIntent);  
  43.           
  44.         tabHost.addTab(recentContactSpec);  
  45.         tabHost.addTab(contactBookSpec);  
  46.         tabHost.addTab(smsMessageSpec);  
  47.         tabHost.addTab(settingSpec);  
  48.           
  49.         tabHost.setCurrentTab(1);  
  50.     }  
  51.       


这个我没有写Demo,也就不提供详细的Demo了,不过还是截个图吧

 

原创粉丝点击