Android: 更改选项卡标题文字大小

Android: change the tab heading text size

您好,我使用 TabHost 创建了滑动标签。这是我的代码

代码

SwipeActivity.java(主要活动)

package com.app.swipeex;

import java.util.HashMap;
import java.util.List;
import java.util.Vector;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;

public class SwipeActivity extends FragmentActivity implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {

    private TabHost mTabHost;
    private ViewPager mViewPager;
    private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, SwipeActivity.TabInfo>();
    private PagerAdapter mPagerAdapter;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Inflate the layout
        setContentView(R.layout.activity_swipe);
        // Initialise the TabHost
        this.initialiseTabHost(savedInstanceState);
        if (savedInstanceState != null) {
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state
        }
        // Intialise ViewPager
        this.intialiseViewPager();
    }


    private class TabInfo {
        private String tag;
        private Class<?> clss;
        private Bundle args;
        private Fragment fragment;
        TabInfo(String tag, Class<?> clazz, Bundle args) {
            this.tag = tag;
            this.clss = clazz;
            this.args = args;
        }

   }


   class TabFactory implements TabContentFactory {

       private final Context mContext;

       /**
        * @param context
        */
       public TabFactory(Context context) {
           mContext = context;
       }
       public View createTabContent(String tag) {
           View v = new View(mContext);
           v.setMinimumWidth(0);
           v.setMinimumHeight(0);
           return v;
       }

   }
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("tab", mTabHost.getCurrentTabTag()); //save the tab selected
        super.onSaveInstanceState(outState);
    }

    private void intialiseViewPager() {

        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this, Tab1Activity.class.getName()));
        fragments.add(Fragment.instantiate(this, Tab2Activity.class.getName()));
        fragments.add(Fragment.instantiate(this, Tab3Activity.class.getName()));
        fragments.add(Fragment.instantiate(this, Tab4Activity.class.getName()));
        this.mPagerAdapter  = new PagerAdapter(super.getSupportFragmentManager(), fragments);
        //
        this.mViewPager = (ViewPager)super.findViewById(R.id.viewpager);
        this.mViewPager.setAdapter(this.mPagerAdapter);
        this.mViewPager.addOnPageChangeListener(this);
    }

    private void initialiseTabHost(Bundle args) {
        mTabHost = (TabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup();
        TabInfo tabInfo = null;
        SwipeActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab1").setIndicator("Discussion"), ( tabInfo = new TabInfo("Tab1", Tab1Activity.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        SwipeActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab2").setIndicator("About"), ( tabInfo = new TabInfo("Tab2", Tab2Activity.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        SwipeActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab3").setIndicator("Photo"), ( tabInfo = new TabInfo("Tab3", Tab3Activity.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        SwipeActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab4").setIndicator("Camera Setting"), ( tabInfo = new TabInfo("Tab4", Tab4Activity.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);


        mTabHost.setOnTabChangedListener(this);
    }

    private static void AddTab(SwipeActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
        // Attach a Tab view factory to the spec
        tabSpec.setContent(activity.new TabFactory(activity));
        tabHost.addTab(tabSpec);
    }

    public void onTabChanged(String tag) {
        //TabInfo newTab = this.mapTabInfo.get(tag);
        int pos = this.mTabHost.getCurrentTab();
        this.mViewPager.setCurrentItem(pos);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageSelected(int position) {
        // TODO Auto-generated method stub
        this.mTabHost.setCurrentTab(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        // TODO Auto-generated method stub

    }
}

activity_swipe.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:orientation="horizontal" />

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0" />

            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />
        </LinearLayout>
    </TabHost>

</LinearLayout>

我终于创建了四个片段

Tab1Activity.java, Tab2Activity.java, Tab3Activity.java, Tab4Activity.java

package com.app.swipeex;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Tab1Activity extends Fragment{

    View tab1;

    @Override
    @Nullable
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        tab1 = inflater.inflate(R.layout.tab1, container, false);

        return tab1;
    }
}

通过使用这段代码,我得到了类似

的输出

我想更改这段代码中的两件事

  1. 讨论和摄像头设置的对齐方式有误。为此,我想将此文本设置为单行(我尝试更改这些文本的大小,但我没有这样做)

  2. 我收到的所有文字都是大写的。所以我想按原样设置标题(只有第一个字母大写,其余全部小写)

这样改..

 TextView textview = (TextView)  mTabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title);
textview.setTextSize(25);