在 Android 中自定义状态选项卡
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/773690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Customizing tabs on state in Android
提问by Chrispix
I know how to put the icon on each tab, that is no problem. I also ran across this : [Stack Overflow thread on pretty much same thing][1]
我知道如何将图标放在每个选项卡上,这没问题。我也遇到过这个问题:[Stack Overflow thread on几乎相同的事情][1]
I followed one of the links from that question and found [this][2]
我按照那个问题的链接之一找到了 [this][2]
Pretty much, it said to use a selector defined in the XML, sure, did that. But there is no id associated w/ it so I am not sure how to get the selector function as a drawable so I can use it as the icon for the tabs. Maybe I am going about this the wrong way. But this is what I have, and obviously missing something.
几乎,它说使用在 XML 中定义的选择器,当然,做到了。但是没有与它相关联的 id,所以我不确定如何将选择器功能作为可绘制对象使用,以便我可以将其用作选项卡的图标。也许我以错误的方式解决这个问题。但这就是我所拥有的,显然缺少一些东西。
<selector
android:id="@+id/myselector"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item
android:state_focused="false"
android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/darklogo" />
<item
android:state_focused="false"
android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/lightlogo" />
<!-- Focused states -->
<item
android:state_focused="true"
android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/lightlogo" />
<item
android:state_focused="true"
android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/lightlogo" />
<!-- Pressed -->
<item
android:state_pressed="true"
android:drawable="@drawable/lightlogo" />
</selector>
In my code, an example tab is generated using :
在我的代码中,使用以下命令生成示例选项卡:
host.addTab(host.newTabSpec("three")
.setIndicator("map",drawables)
.setContent(new Intent(this, Map.class)));
Right now drawable is just a reference to a drawable image resource. How do I make the selector a drawable?
现在 drawable 只是对可绘制图像资源的引用。如何使选择器成为可绘制的?
This is my question [1]: Updating Android Tab Icons[2]: http://groups.google.com/group/android-evelopers/browse_thread/thread/ef3bdebcb715b385
这是我的问题 [1]:更新 Android 选项卡图标[2]:http: //groups.google.com/group/android-evelopers/browse_thread/thread/ef3bdebcb715b385
回答by Reto Meier
The XML you've included here is a way of defining a drawable that lets you embed a case statement. It presents a different drawable depending on the state of the View it's being assigned to. As a drawable, you should save it as an xml file within the res/drawable
folder of your project (for example tabselector.xml
).
您在此处包含的 XML 是一种定义可让您嵌入 case 语句的可绘制对象的方法。它根据分配给它的视图的状态呈现不同的可绘制对象。作为可绘制对象,您应该将其保存为res/drawable
项目文件夹中的 xml 文件(例如tabselector.xml
)。
To use it for the Tabhost, you need to construct the TabActivity as you normally would (as shown in this tutorial example).
要将其用于 Tabhost,您需要像往常一样构造 TabActivity(如本教程示例所示)。
Then when you add each tab to the host, you specify the tabselector
drawable as the indicator as shown for "TAB 1" below.
然后,当您将每个选项卡添加到主机时,您将tabselector
可绘制对象指定为指示符,如下面的“TAB 1”所示。
Drawable mySelector = getResources().getDrawable(R.drawable.tabselector);
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1", mySelector).setContent(R.id.textview1));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.textview2));
Note: You cannotchange the color of the tab backgrounds behind the icons at this point.
注意:此时您无法更改图标后面的选项卡背景颜色。
回答by Moss
You could use a View as an indicator, this way you can customize it the way you wish.
您可以使用视图作为指示器,这样您就可以按照您希望的方式对其进行自定义。
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator(View MyView).setContent(R.id.textview1));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.textview2));
The first tab will use a View as its indicator and the second a CharSequence. Have a look at the actual TabSpec class (http://developer.android.com/reference/android/widget/TabHost.TabSpec.html).
第一个选项卡将使用一个视图作为其指示器,第二个选项卡将使用一个 CharSequence。看看实际的 TabSpec 类(http://developer.android.com/reference/android/widget/TabHost.TabSpec.html)。