【发布时间】:2012-01-12 23:32:34
【问题描述】:
我想在 android 运行时通过单击该选项卡来更改选项卡视图。
当我点击“收藏夹”选项卡时,该选项卡图像会自动显示为“主页”选项卡..
请帮帮我。
像这样 之前
之后
请举例...
在此先感谢...
【问题讨论】:
我想在 android 运行时通过单击该选项卡来更改选项卡视图。
当我点击“收藏夹”选项卡时,该选项卡图像会自动显示为“主页”选项卡..
请帮帮我。
像这样 之前
之后
请举例...
在此先感谢...
【问题讨论】:
请参阅下面的链接:
http://developer.android.com/guide/practices/ui_guidelines/icon_design_tab.html
要在选择或取消选择选项卡时自动更改图像,您可以使用以下 xml 代码:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- selected state -->
<item android:drawable="@drawable/ic_tab_friends_selected"
android:state_selected="true"
android:state_pressed="false" />
<!-- unselected state (default) -->
<item android:drawable="@drawable/ic_tab_friends_unselected" />
</selector>
【讨论】:
要在选项卡状态更改时自动在选项卡图标图像之间切换,请使用放置在资源的可绘制文件夹中的 xml 文件作为选项卡指示器资源。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="@drawable/home" android:state_selected="true" />
<!-- When not selected, use white-->
<item android:drawable="@drawable/favorites" />
</selector>
请查看Tab Layout
【讨论】:
我有一个示例,说明与您的要求不同,但在更改某些条件或添加某些条件后使用此 OR,您将得到解决方案。
如果我们想在运行时更改标签颜色或图像或文本颜色,那么我们使用此代码。
for(int i=0;i<host.getTabWidget().getChildCount();i++)
{
host.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab_bg);
TextView tv = (TextView) host.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
tv.setTextColor(Color.parseColor("#ffffff"));
}
host.getTabWidget().getChildAt(host.getCurrentTab()).setBackgroundResource(R.drawable.tab_active_bg);// selected
TextView tv = (TextView) host.getCurrentTabView().findViewById(android.R.id.title); //for Selected Tab
tv.setTextColor(Color.parseColor("#000000"));
在此代码中,host.getTabWidget().getChildCount() 返回标签小部件的总数。例如,当您显示图像时,这里有 4 个选项卡。
希望对你有帮助。祝你好运。 :)
【讨论】: