如何将样式应用于onTabChanged中TabHost的选项卡?

时间:2015-04-13 07:33:46

标签: android

有一个TabHost

<?xml version="1.0" encoding="utf-8"?>
<TabHost 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/onglets"
    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:orientation="horizontal"
            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">
            <include
                android:id="@+id/contentOnglet1" 
                layout="@layout/parcelle"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
            <include
                android:id="@+id/contentOnglet2"
                layout="@layout/liste_modele"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
        </FrameLayout>
    </LinearLayout>
</TabHost>

Java代码:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle data = getIntent().getExtras();
        if (!data.isEmpty()) {
            label1 = data.getString(getResources().getString(R.string.extra_label_onglet1));
            label2 = data.getString(getResources().getString(R.string.extra_label_onglet2));
            data.clear();
        }
        setContentView(R.layout.tabHostLayout);
        mTabHost = (TabHost)findViewById(R.id.onglets);
        mTabHost.setup();
        afficherOnglet1();
        afficherOnglet2();
        mTabHost.setCurrentTab(0);
    }

    private void afficherOnglet1() {
        TabHost.TabSpec onglet1 = mTabHost.newTabSpec("onglet1");
        View onglet = getLayoutInflater().inflate(R.layout.template_tab_onglets, null);
        TextView label = (TextView) onglet.findViewById(R.id.tabLabel);
        label.setText(label1);
        onglet1.setIndicator(onglet);
        View titre = (View)findViewById(R.id.title_tv_layout);
        titre.setVisibility(View.GONE);
        onglet1.setContent(R.id.contentOnglet1);
        mTabHost.addTab(onglet1);
    }

    private void afficherOnglet2() {
        TabHost.TabSpec onglet2 = mTabHost.newTabSpec("onglet2");
        View onglet = getLayoutInflater().inflate(R.layout.template_tab_onglets, null);
        TextView label = (TextView) onglet.findViewById(R.id.tabLabel);
        label.setText(label2);
        onglet2.setIndicator(onglet);
        onglet2.setContent(R.id.contentOnglet2);
        mTabHost.addTab(onglet2);
    }

template_tab_onglets.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
        <TextView android:id="@+id/tabLabel"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:paddingTop="@dimen/paddingTitre"
            android:paddingBottom="@dimen/paddingTitre"
            android:background="@drawable/contour_tab_onglet"
            android:textColor="@color/txt_color"
        />
</LinearLayout>

onTabChangedListener的{​​{1}}中,我想将TabHost应用于style tabLabel,例如粗体样式。怎么做?

1 个答案:

答案 0 :(得分:2)

这是方式

mTabHost.setOnTabChangedListener(new OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabName) {

            Log.i("***Selected Tab", "Im currently in tab with index::" + tabHost.getCurrentTab());

            View v=tabHost.getCurrentTabView();
            TextView title2 = (TextView) v.findViewById(R.id.tabLabel);
            title2.setTypeface(Typeface.DEFAULT_BOLD);

     }
    });