【问题标题】:TabHost Android make Text BoldTabHost Android 使文本加粗
【发布时间】:2013-01-01 04:05:20
【问题描述】:

我正在寻找文本加粗的方法,基本上是 Tab Host 使文本加粗。

示例图片如下

下面是我的代码。

tabHost.setOnTabChangedListener(new OnTabChangeListener()
    {
        @Override
        public void onTabChanged(String tabId) 
        {
            if (tabId.equals("READING")) 
            {
                for(int i=0;i< tabHost.getTabWidget().getChildCount();i++)
                {
                    //unselected
                    tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#E6A9EC")); 
                }
                // selected
                tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.WHITE);
            }
            else
            {
                for(int i=0;i< tabHost.getTabWidget().getChildCount();i++)
                {
                 //unselected
                    tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#E6A9EC"));
                }
                // selected
                tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.WHITE);
            }
        }
   });

【问题讨论】:

  • 你的意思是把选中的项目加粗还是全部加粗?

标签: android android-layout android-intent android-style-tabhost


【解决方案1】:

您可以在自定义主题更改中设置样式

<item name="android:tabWidgetStyle">@android:style/Widget.TabWidget</item> 

<style name="Widget.TabWidget">
        <item name="android:textAppearance">@style/TextAppearance.Widget.TabWidget</item>
        <item name="android:ellipsize">marquee</item>
        <item name="android:singleLine">true</item>
</style>  


<style name="TextAppearance.Widget.TabWidget">
    <item name="android:textSize">14sp</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textColor">@android:color/tab_indicator_text</item>
</style>   

【讨论】:

  • 你能更清楚我应该在哪里添加代码吗?我在styles.xml 中做到了,但它不起作用。有没有办法在 JAVA 中做到这一点?感谢您的帮助
【解决方案2】:

我发现这是最简单的方法:
将选项卡添加到 TabHost 后,请执行以下操作:

TextView textView = (TextView) tabHost.getTabWidget().getChildAt(i)
                .findViewById(android.R.id.title); 

'i' 只是新标签的索引。就我而言,我使用的是 for 循环。 之后就很简单了,这样做:

textView .setTypeface(null, Typeface.BOLD);

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-15
    • 2020-04-27
    • 2016-06-07
    • 2014-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    相关资源
    最近更新 更多