分享

android tabHost

 印度阿三17 2019-07-10

我有2个关于tabHost的问题:
我用2个标签创建了tabHost
对于标签标题,我使用setIndicator(TextView)
(我使用api 4级)
我的标题背景是白色的.我使用选择器作为标题在标题的差异图像之间进行选择.

>我想仅在选择/按下时使标题文本变为粗体.我没有成功使用我拥有的选择器.我能做到吗?我的想法是,在我使用drawable a的情况下,我想要文本粗体.其他案件不大胆.关于textColor的同样问题.
>它看起来像一个错误 – 当标签首次打开时,所选标签上的文本(我在tabHost.setCurrentTab(tabId)中使用的文本)根本看不到.在第一次按下/聚焦/聚焦任何其他项目后,它看起来很好.任何想法为什么或如何解决这个问题?

提前致谢

on tabActivity –

TextView title1 = new TextView(MainActivity.getInstnace(), null, android.graphics.Typeface.NORMAL);
TextView title2 = new TextView(MainActivity.getInstnace(), null, android.graphics.Typeface.NORMAL);

title1.setText("teb11 title");
title1.setBackgroundResource(R.drawable.tabtitle);
title1.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.tab1), null, null, null);

title2.setText("tab22 title");
title2.setBackgroundResource(R.drawable.tabtitle);

title2.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.tab2), null, null, null);

TabSpec tab1 = mTabHost.newTabSpec("tab1").setIndicator(title1).setContent(R.id.list1);
TabSpec tab2 = mTabHost.newTabSpec("tab2").setIndicator(title2).setContent(R.id.list2);

mTabHost.addTab(tab1);
mTabHost.addTab(tab2);      
mTabHost.setCurrentTab(0);

选择器tab1.xml

<selector xmlns:android="http://schemas./apk/res/android">  
    <item android:state_selected="true"     
    android:drawable="@drawable/iconselect"/>   
    <item android:state_pressed="true"  
    android:drawable="@drawable/iconselect"/>   
    <item android:drawable="@drawable/icon"/>        
</selector> 

tabTitle的选择器

<selector xmlns:android="http://schemas./apk/res/android">  
    <item android:state_pressed="true"  
    android:drawable="@drawable/tabselected"/> 
    <item android:state_selected="true"     
    android:drawable="@drawable/tab" />  
    <item android:state_focused="true"  
    android:drawable="@drawable/tab" />  
</selector> 

解决方法:

对于你的问题#1:
在你的TabsAdapter上使用onPageSelected(int position),如下所示:

 public void onPageSelected(int position) {
      //your logic here
      fixTitleText();
 }

和:

 private void fixTitleText() {
      for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i  ) {
           View view = mTabHost.getTabWidget().getChildAt(i);
           TextView tv = (TextView) view.findViewById(android.R.id.title);
           tv.setTextColor(getResources().getColor(R.drawable.text_selector));
           tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
           tv.setTypeface(null, Typeface.BOLD);
      }
 }

请注意,此代码适用于HONEYCOMB及更高版本,在此之前,选项卡主机视图层次结构略有不同

来源:https://www./content-4-314351.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约