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

标签 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>

TabHostonTabChangedListener 中,我想将 style 应用于 TextView tabLabel ,一个粗体风格例如。怎么做?

最佳答案

方法是这样的

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);

     }
    });

关于android - 如何在 onTabChanged 中将样式应用于 TabHost 的选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29600046/

相关文章:

android - 无法解析变量 '${project.groupId}'

android - Activity 泄露了最初添加在这里的窗口 com.android.internal.policy.impl.PhoneWindow$DecorView@44f72ff0

Android RecyclerView设置选中项背景

java - 在 ListFragment 中使用 asset 文件夹中的图像而不是可绘制文件夹

HTC Thunderbolt 上的 Android 电子邮件多个附件问题

android - 应用程序在同一型号的两部手机中显示不同的文字大小

java - 崩溃 : com. badlogic.gdx.physics.box2d.World.jniCreateBody

android - 如何在 Android 中使用 Place Autocomplete intent builder 获取 Place 的开放时间?

android - 可以删除 recyclerview 中以前的项目吗?

android - 如何为 CalendarView 设置 OnClickListener?