android tabHost
我有两个关于 tabHost 的问题: 我创建了带有 2 个选项卡的 tabHost 对于选项卡标题,我使用 setIndicator(TextView) (我使用 API 级别 4) 我的标题背景是白色的。我使用标题选择器在标题的差异图像之间进行选择。
我想仅在选择/按下时使标题文本变为粗体。我没有成功使用我拥有的选择器来做到这一点。我能做到吗?这个想法是,在我使用可绘制的情况下,我希望文本加粗。其他情况不加粗。关于 textColor 的同样问题。
它看起来像一个错误 - 当选项卡第一次打开时,所选选项卡上的文本(我在 tabHost.setCurrentTab(tabId) 中使用的文本)根本看不到。第一次按下/聚焦/聚焦任何其他项目后,它看起来不错。知道为什么或如何解决这个问题吗?
提前感谢
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.android.com/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.android.com/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>
I have 2 questions regarding tabHost:
I've created tabHost with 2 tabs
and for the tab titles I use setIndicator(TextView)
(I work with api level 4)
my title background is white. I use selector for the title to choose between diff images for the title.
I want to make the title text bold only when selected/pressed. I didn't succeed to do it using the selector I have. can I do it at all? the idea is that on cases I use drawable a I want the text bold. other cases not bold. same question regarding textColor.
it looks like a bug - when the tab first opens, the text on the selected tab (the one I used in tabHost.setCurrentTab(tabId)) is not seen at all. after first press/focus/focus any other item it looks well. any idea why or how to solve this?
thanks in advance
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);
the selector tab1.xml
<selector xmlns:android="http://schemas.android.com/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>
the selector for tabTitle
<selector xmlns:android="http://schemas.android.com/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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于你的问题#1:
在您的
TabsAdapter
上使用onPageSelected(intposition)
像这样:并且:
只需注意此代码适用于 HONEYCOMB 及以上,在此之前选项卡主机视图层次结构有点不同
For your question #1:
On your
TabsAdapter
useonPageSelected(int position)
like this:and:
just notice that this code is for HONEYCOMB and up, before this the tab host view hierarchy is a bit different
这是我的 xml
this is my xml
我可以查看您的布局 XML 还是您仅通过 Java 命令执行此操作。不管怎样,我希望看到的不仅仅是一些伪代码来实际解决这个问题。
那将是您的 main.xml 文件或您的 OnCreate() 函数。如果您通过 XML 设置布局,则 main.xml 应该具有类似于以下内容的内容:
可以完全通过 Java 命令完成此操作,但尤其是在修复 #2 时,我们可能需要查看更多代码。作为 OnCreate() 函数的一部分,我建议初始化您想要显示的所有内容。否则,如果它不在您的 main.xml 中,并且未初始化,则不会显示。
Can I see your layout XML or are you doing this only through Java commands. Either way, I'd like to see more than just some pseudocode to actually work on this.
That would be either your main.xml file or your OnCreate() function. IF you are setting your layout through XML, main.xml should have contents similar to :
It is possible to do it entirely through Java commands, but especially when fixing #2, we will likely need to see more of your code. As part of the OnCreate() function, I would recommend initializing all the stuff you would like to be shown. Otherwise, if it is not in your main.xml, and it is not initialized, it will not be shown.
如果想要在选择时使用粗体文本,
可能有不同的方法,但我所做的是...
制作自定义 TextView,然后处理触摸事件,如向下操作时加粗,并在向上操作时设置为正常。
If want the bold text on selection,
There may a different ways but what I did is...
to make custom TextView, and then Handle the touch event Like bold on action down and set to normal on action up.