android选项卡+自定义字体和样式

在布局文件中,首先添加一个TabHost组件,然后在该组件中添加线性布局管理器,并且在该布局中添加一个作为标签组的TabWidget和一个作为标签内容的FrameLayout组件。
res/layout/main.xml:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:id="@android:id/tabhost"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent">  
    <LinearLayout  
        android:orientation="vertical"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent">  
        <TabWidget  
            android:id="@android:id/tabs"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"/>  
        <FrameLayout  
            android:id="@android:id/tabcontent"  
            android:layout_width="fill_parent"  
            android:layout_height="fill_parent">  
        </FrameLayout>  
    </LinearLayout>  
</TabHost>  

编写各标签页中要显示内容对应的XML布局文件:
res/layout/tab1.xml:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:id="@+id/linearLayout1"  
    android:orientation="vertical" >  
    <TextView  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="界面1"/>  
    <TextView  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="欢迎来到界面1"/>  
</LinearLayout>  

res/layout/tab2.xml:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:id="@+id/linearLayout2"  
    android:orientation="vertical" >  
    <TextView  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="界面2"/>  
    <TextView  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="欢迎来到界面2"/>  
</LinearLayout>  

Activity代码:

protected void onCreate(){
    tabHost.setup();
    LayoutInflater inflater = LayoutInflater.from(mContext);
    inflater.inflate(R.layout.tab1_fxjk,tabHost.getTabContentView());
    inflater.inflate(R.layout.tab2_fxjk,tabHost.getTabContentView());
    tabHost.addTab(tabHost.newTabSpec("tab01")
            .setIndicator("航班列表")
            .setContent(R.id.linearLayout1));//添加第一个标签页
    tabHost.addTab(tabHost.newTabSpec("tab02")
            .setIndicator("航班信息")
            .setContent(R.id.linearLayout2));//添加第二个标签页

    tabHost.setCurrentTabByTag("航班列表"); // 设置第一次打开时默认显示的标签,
    updateTab(tabHost);//初始化Tab的颜色,和字体的颜色
    tabHost.setOnTabChangedListener(new OnTabChangedListener()); // 选择监听器
}

class OnTabChangedListener implements TabHost.OnTabChangeListener {

        @Override
        public void onTabChanged(String tabId) {
            tabHost.setCurrentTabByTag(tabId);
            System.out.println("tabid " + tabId);
            System.out.println("curreny after: " + tabHost.getCurrentTabTag());
            updateTab(tabHost);
        }
    }

    /**
     * 更新Tab标签的颜色,和字体的颜色
     * @param tabHost
     */
    private void updateTab(final TabHost tabHost) {
        for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
            View view = tabHost.getTabWidget().getChildAt(i);
            TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
            tv.setTextSize(9);
            tv.setTypeface(Typeface.SERIF, 2); // 设置字体和风格
            if (tabHost.getCurrentTab() == i) {//选中
                view.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.btn_y));//选中后的背景
                tv.setTextColor(mContext.getResources().getColorStateList(
                        android.R.color.black));
            } else {//不选中
                view.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.btn_b));//非选择的背景
                tv.setTextColor(mContext.getResources().getColorStateList(
                        android.R.color.white));
            }
        }
    }

效果如下:
这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值