【问题标题】:Android: Change Tab Text Color ProgrammaticallyAndroid:以编程方式更改选项卡文本颜色
【发布时间】:2011-07-31 11:10:17
【问题描述】:

我有一个这样的 TabHost:

<?xml version="1.0" encoding="utf-8"?>
 <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabhost"
android:background="@drawable/tabs_bg">

<LinearLayout 
    android:id="@+id/LinearLayout01"
    android:orientation="vertical" 
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">
    <TabWidget 
        android:id="@android:id/tabs"
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent"
        android:layout_marginBottom="5dip">
    </TabWidget>
    <FrameLayout 
        android:id="@android:id/tabcontent"
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent">
    </FrameLayout>
</LinearLayout>

我正在以编程方式向此 TabHost 添加选项卡,如下所示:

tabHost = (TabHost)findViewById(android.R.id.tabhost);
    tabHost.setOnTabChangedListener(this);

    /* tid1 is firstTabSpec Id. Its used to access outside. */
    TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
    TabSpec secondTabSpec = tabHost.newTabSpec("tid2");
    TabSpec ThirdTabSpec = tabHost.newTabSpec("tid3");

    /* TabSpec setIndicator() is used to set name for the tab. */
    /* TabSpec setContent() is used to set content for a particular tab. */
    firstTabSpec.setIndicator("Tab1", getResources().getDrawable(R.drawable.tab1));
    secondTabSpec.setIndicator("Tab2", getResources().getDrawable(R.drawable.tab2));
    ThirdTabSpec.setIndicator("Tab3", getResources().getDrawable(R.drawable.tab3));

    firstTabSpec.setContent(new Intent(this,FirstTab.class));
    secondTabSpec.setContent(new Intent(this,SecondTab.class));
    ThirdTabSpec.setContent(new Intent(this,ThirdTab.class));

    /* Add tabSpec to the TabHost to display. */
    tabHost.addTab(firstTabSpec);
    tabHost.addTab(secondTabSpec);
    tabHost.addTab(ThirdTabSpec);

    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
    {
        tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#121312"));
    }

    tabHost.getTabWidget().setCurrentTab(0);
    tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#f1a026"));

这里是 onTabChanged 事件:

public void onTabChanged(String tabId) {
    // TODO Auto-generated method stub
    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
    {
        tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#121312"));
    } 

    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#f1a026"));
}

在 onTabChanged 事件中,我还想更改所有选项卡的文本颜色。请帮助我如何更改事件中标签的文本颜色?

谢谢,

【问题讨论】:

    标签: android android-widget


    【解决方案1】:

    要更改选项卡的文本颜色,您需要获取视图,即设置为选项卡标题的 TextView,您可以这样更改:

        TabHost tabhost = getTabHost();
        for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) 
        {
            TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
            tv.setTextColor(.....);
        } 
    

    希望这会有所帮助....

    【讨论】:

    • 嗨法尔汉!在这种情况下 android.R.id.title 是什么?
    • @Farhan,我的代码中没有任何 id 为“title”的内容。请检查我的代码并告诉我应该用什么替换 android.R.id.title?
    • 正如我所说,当使用选项卡时,它会由框架自动添加......就像您在 setIndicator(第一个参数)中设置文本时一样,此文本设置在 id 为 android 的 textview 上。 R.id.title.... 只需获取它并更改颜色...
    • @Farhan,非常感谢,它解决了我的问题。我以前也试过这个,但在我以codetabHost=(TabHost)findViewById(android.R.id.tabhost);code 访问我的 TabHost 之前,然后尝试按照您的描述更改颜色,但我的应用程序总是崩溃.现在,我将此语句更改为 codetabHost=getTabHost();code 并且它现在工作正常。我无法理解两者之间的区别。你能告诉我这两者之间的区别吗?谢谢
    • 用你的代码试试,但有一点改变......即 tabHost = (TabHost)findViewById(R.id.tabhost);和 c 如果它有效......如果是,那么你应该明白我想告诉的点......
    【解决方案2】:

    为新设计支持标签布局;你可以在你的xml中定义它
    app:tabTextColor="@color/tab_inactive" app:tabSelectedTextColor="@color/tab_active"
    例如-

    <android.support.design.widget.TabLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/tabanim_tabs"
                app:tabTextColor="@color/tab_inactive"
                app:tabSelectedTextColor="@color/tab_active"
                android:textAllCaps="false"
                />
    


    以编程方式可以这样实现:

    tabLayout.setTabTextColors(ContextCompat.getColorStateList(this, R.color.tab_selector));
            tabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.indicator));
    

    【讨论】:

    • 你能发布一个示例 sn-p 显示在哪里添加这个吗?我在 Android Studio 中找不到这样的选项
    • 完美答案。虽然这是一个不同的问题,但我只想指出我们在示例中看到的 textAllCaps="false" 目前没有效果,要实现删除 CAP 的行为,您需要创建一个新样式并将其分配给 tablayout ,看看下面的问题stackoverflow.com/questions/31295116/…
    • 感谢您指出@A.Alqdomi。我从我的工作示例中粘贴了代码 sn-p,这样代码就在那里,与答案无关。
    • 有没有办法在没有 stateList 的情况下改变颜色?
    【解决方案3】:

    对我来说,@Farhan 的解决方案不起作用,因为 getChildCount() 在有四个选项卡时不断返回 1。使用getTabCount()getChildTabViewAt() 为我解决了这个问题:

    for (int tabIndex = 0 ; tabIndex < mTabHost.getTabWidget().getTabCount() ; tabIndex ++) {
        View tab = mTabHost.getTabWidget().getChildTabViewAt(tabIndex);
        TextView t = (TextView)tab.findViewById(android.R.id.title);
        t.setTextColor(getResources().getColor(R.color.white));
    }
    

    我以为我为有同样问题的人发布了这个替代方案。

    【讨论】:

      【解决方案4】:

      当您使用 findViewById(id) 时,您要求系统查找 ID 为“id”的任何视图相对与当前 ViewGroup。这意味着您在代码中所做的是this.findViewById(id),因此它将在当前视图中查找“id”。并且做 findViewById(android.R.id.tabHost) 不是很聪明,因为它根本不存在......

      但是,当您执行 getTabHost() 时,您要求系统获取您的活动的 unique tabHost,无论它是否有任何 View 作为 root,即 tabHost 可以附加到它上面的任何内容.

      作为结论,您应该始终在您的 TabHostActivity 中使用我们 getTabHost

      希望我清楚

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-17
        • 1970-01-01
        • 1970-01-01
        • 2013-04-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多