gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 20:49:14 26 4
gpt4 key购买 nike

有一个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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com