Tabhost 居底实现

来源:互联网 发布:筛选两个表格不同数据 编辑:程序博客网 时间:2024/04/27 17:22

有的时候我们要实现Tabhost的功能时需要将tab标签居底实现,如腾讯微博、新浪微博的UI都是这样实现的。那么如何实现呢,这里用到了RadioGroup来实现该功能。

TabHostActivity.java

public class TabHostActivityextends Activityimplements

       OnCheckedChangeListener{

    private TabHosttabHost;

 

    private IntentcertificateIntent;

    private IntentfeeIntent;

    private IntentscoreIntent;

    private IntentstudyIntent;

    private IntentmoreIntent;

 

    @Override

    publicvoid onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.tab);

       // tabHost = getTabHost();

       tabHost = (TabHost) findViewById(R.id.my_tabhost);

       LocalActivityManager groupActivity =

new LocalActivityManager(this,false);

       groupActivity.dispatchCreate(savedInstanceState);

       tabHost.setup(groupActivity);

       initIntent();

       addSpec();

      ((RadioGroup) findViewById(R.id.tab_radiogroup))

             .setOnCheckedChangeListener(this);

 

    }

 

    /**

     *初始化各个tab标签对应的intent

     */

    privatevoid initIntent() {

       studyIntent =new Intent(this, StudyActivity.class);

       scoreIntent =new Intent(this, ScoreActivity.class);

       feeIntent =new Intent(this, FeeActivity.class);

       certificateIntent =new Intent(this, CertificateActivity.class);

       moreIntent =new Intent(this, MoreActivity.class);

    }

 

    /**

     *tabHost添加各个标签项

     */

    privatevoid addSpec() {

tabHost.addTab(this.buildTagSpec("tab_study", R.string.study_progress,

              R.drawable.account01,studyIntent));

tabHost.addTab(this.buildTagSpec("tab_score", R.string.test_score,

              R.drawable.account02,scoreIntent));

       tabHost.addTab(this.buildTagSpec("tab_fee", R.string.fee_pay,

              R.drawable.account03,feeIntent));

       tabHost.addTab(this.buildTagSpec("tab_certificate",

              R.string.certificate_grant, R.drawable.account04,

              certificateIntent));

       tabHost.addTab(this.buildTagSpec("tab_more", R.string.more,

              R.drawable.account05,moreIntent));

    }

 

    /**

     *自定义创建标签项的方法

     *@param tagName标签标识

     *@param tagLable标签文字

     *@param icon标签图标

     *@param content标签对应的内容

     *@return

     */

    private TabHost.TabSpec buildTagSpec(String tagName,int tagLable,

           int icon, Intent content) {

       returntabHost

              .newTabSpec(tagName)

              .setIndicator(getResources().getString(tagLable),

                     getResources().getDrawable(icon)).setContent(content);

    }

 

    @Override

    public void onCheckedChanged(RadioGroup group, int checkedId) {

      switch (checkedId) {

      case R.id.radio_button_study:

          tabHost.setCurrentTabByTag("tab_study");

          break;

      case R.id.radio_button_score:

          tabHost.setCurrentTabByTag("tab_score");

          break;

      case R.id.radio_button_certificate:

          tabHost.setCurrentTabByTag("tab_certificate");

          break;

      case R.id.radio_button_fee:

          tabHost.setCurrentTabByTag("tab_fee");

          break;

      case R.id.radio_button_more:

          tabHost.setCurrentTabByTag("tab_more");

          break;

      }

 

    }

}

tab.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<TabHostandroid:id="@+id/my_tabhost"android:layout_width="fill_parent"

         android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayoutandroid:orientation="vertical"

android:layout_width="fill_parent"android:layout_height="fill_parent">

       <FrameLayoutandroid:id="@android:id/tabcontent"

                    android:layout_width="fill_parent"android:layout_height="0.0dip"

                      android:layout_weight="1.0"/>

<TabWidgetandroid:id="@android:id/tabs"android:visibility="gone"

android:layout_width="fill_parent"android:layout_height="wrap_content"

                    android:layout_weight="0.0"/>

       <RadioGroup android:id="@+id/tab_radiogroup"

             android:background="@drawable/tabs_bg" android:layout_width="fill_parent"

         android:layout_height="wrap_content" android:gravity="center_vertical"

         android:layout_gravity="bottom" android:orientation="horizontal">

           <RadioButtonandroid:id="@+id/radio_button_study"

              android:layout_marginTop="2.0dip"android:text="学习进度"

              android:drawableTop="@drawable/account01"style="@style/tab_button_bottom"

              android:checked="true"/>

           <RadioButtonandroid:id="@+id/radio_button_score"

              android:layout_marginTop="2.0dip"android:text="考试成绩"

              android:drawableTop="@drawable/account02"style="@style/tab_button_bottom"/>

           <RadioButtonandroid:id="@+id/radio_button_certificate"

              android:layout_marginTop="2.0dip"android:text="证书发放"

              android:drawableTop="@drawable/account03"style="@style/tab_button_bottom"/>

           <RadioButtonandroid:id="@+id/radio_button_fee"

              android:layout_marginTop="2.0dip"android:text="费用缴纳"

              android:drawableTop="@drawable/account04"style="@style/tab_button_bottom"/>

           <RadioButtonandroid:id="@+id/radio_button_more"

              android:layout_marginTop="2.0dip"android:text="更多"

              android:drawableTop="@drawable/account05"style="@style/tab_button_bottom"/>

       </RadioGroup>

    </LinearLayout>

</TabHost>

styles.xml

<?xmlversion="1.0"encoding="utf-8"?>

<resources>

    <!-- TabHost标签按钮样式 -->

    <stylename="tab_button_bottom">

       <itemname="android:textSize">12px</item>

       <itemname="android:textColor">#ffffffff</item>

       <itemname="android:ellipsize">marquee</item>

       <itemname="android:gravity">center_horizontal</item>

       <itemname="android:background">@drawable/tab_btn_bg</item>

       <itemname="android:layout_marginTop">2.0dip</item>

       <itemname="android:button">@null</item>

       <itemname="android:paddingTop">6dip</item>

       <itemname="android:drawablePadding">4dip</item>

       <itemname="android:layout_width">fill_parent</item>

       <itemname="android:layout_height">wrap_content</item>

       <itemname="android:layout_weight">1.0</item>

       <itemname="android:singleLine">true</item>

    </style>

   

    <!--页面标题LinearLayout样式 -->

    <stylename="activity_title_background">

    <itemname="android:background">@drawable/title_background</item>

       <itemname="android:layout_width">fill_parent</item>

       <itemname="android:layout_height">wrap_content</item>

       <itemname="android:layout_alignParentTop">true</item>

       <itemname="android:gravity">center</item>

    </style>

   

    <!--界面标题TextView样式 -->

    <stylename="activity_title_text">

       <itemname="android:textSize">14dip</item>

       <itemname="android:textColor">@drawable/white</item>

       <itemname="android:layout_width">wrap_content</item>

       <itemname="android:layout_height">wrap_content</item>

       <itemname="android:gravity">center</item>

    </style>

</resources>

运行结果如下图所示

 

程序重要部分:

1.  红色字体部分。

2.  布局文件tab.xml,可以看到该布局文件中将TabWidget隐藏android:visibility="gone"而以一个RadioGroup取而代之。

3.  为RadioGroup设置OnCheckedChangeListener监听,通过onCheckedChanged方法对各个RadioButton点击事件的处理完成标签切换。

 

其实我当初考虑过为什么要用RadioButton而不用普通的Button。后来通过自己做项目,发现使用RadioGroup有以下优点:

1.另外就是布局上比较方便易懂,不用再去用LinearLayout等布局去包含Button。

2. 我们可以很方便的获得当前选中的标签,当然通过TabHost的tabHost.getCurrentTabTag()和getCurrentTab()也是可以的。

3.设置监听很方便,只需要为RadioGroup设置监听就行了,程序中对应的代码是

((RadioGroup) findViewById(R.id.tab_radiogroup))

             .setOnCheckedChangeListener(this);

   如果用Button的话我们需要为所有的Button一个一个去设置监听,相对来说比较麻烦。

4.  或许最重要的一点是因为RadioButton本身就支持图片和文字的上下布局,只需指定图片和文字是什么就可以了而不需要我们自己去实现这种布局。

<RadioButtonandroid:id="@+id/radio_button_more"

           android:layout_marginTop="2.0dip"

android:text="更多"

           android:drawableTop="@drawable/account05"

           style="@style/tab_button_bottom"/>

当然如果如果RadioButton不能满足我们的项目需求,比如我们不需要图片又不想让文字靠底部显示,而是居中显示,这时我们就可以用其他组件代替RadioButton。其实我们可以通过修改或自定义等方式实现多种漂亮的效果,比如“人人网”手机客户端的个人主页中Tab标签是可以左右滑动的。


转载于:http://www.cnblogs.com/wader2011/archive/2011/10/13/2209668.html


2、注意.xml文件中<FrameLayout>和<TabWidget>的相对位置:

<FrameLayout/>
<TabWidget/>

xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent">        <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">            <!-- 实现Tab标签的居底主要是通过设置属性 android:layout_weight="1" -->            <!-- 还要注意FrameLayout标签的位置,要写在TabWidget标签的前面 -->            <FrameLayout android:layout_width="match_parent" android:layout_weight="1.0" android:layout_height="match_parent" android:id="@android:id/tabcontent">                <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab1">                                 <TextView android:text="我梦见海棠花盛开的声音,宛如夜空中不变的精灵,玄秘生科不大1" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>                                 </LinearLayout>                                  <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab2">                 <TextView android:text="我梦见海棠花盛开的声音,宛如夜空中不变的精灵,玄秘生科不大2" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>                                 </LinearLayout>                                 <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tab3">                 <TextView android:text="我梦见海棠花盛开的声音,宛如夜空中不变的精灵,玄秘生科不大3" android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>                                 </LinearLayout>            </FrameLayout>            <TabWidget             android:layout_gravity="bottom"             android:layout_width="match_parent" android:layout_height="wrap_content"             android:id="@android:id/tabs"></TabWidget>        </LinearLayout>    </TabHost></LinearLayout>

java:

public class Demo_fourActivity extends TabActivity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        TabHost mtabhost=this.getTabHost();        mtabhost.addTab(mtabhost.newTabSpec("one").setIndicator("首页",this.getResources().getDrawable(R.drawable.tabbar_at_hl)).setContent(R.id.tab1));        mtabhost.addTab(mtabhost.newTabSpec("two").setIndicator("信息",this.getResources().getDrawable(R.drawable.tabbar_data_hl)).setContent(R.id.tab2));        mtabhost.addTab(mtabhost.newTabSpec("three").setIndicator("资料",this.getResources().getDrawable(R.drawable.tabbar_main_hl)).setContent(R.id.tab3));              mtabhost.setCurrentTab(1);                    }

截图:



<TabWidget/>
<FrameLayout/>

结果如下: