更改选择选项卡的可绘制性 - android
我在我的一项活动中使用选项卡视图。我想根据选项卡的选择来更改选项卡的可绘制性。所以它是这样的——我有 4 张图像 T11、T12、T21、T22。我想在选择选项卡 1 的情况下初始设置图像 T11 和 T22。现在,我想在选择选项卡 2 后立即将图像更改为 T12 和 T21。
到目前为止,我尝试通过可绘制类型的 xml 文件使用:
drawable for left tab(tab1) --
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
style="?attr/left_active" />
<item android:state_window_focused="false" android:state_enabled="false"
style="?attr/left_inactive" />
<item android:state_pressed="true" android:state_enabled="true"
style="?attr/left_active" />
<item android:state_pressed="true" android:state_enabled="false"
style="?attr/left_inactive" />
</selector>
Drawable for Tab right(Tab2) --
<item android:state_window_focused="false" android:state_enabled="true"
style="?attr/right_active" />
<item android:state_window_focused="false" android:state_enabled="false"
style="?attr/right_inactive" />
<item android:state_pressed="true" android:state_enabled="true"
style="?attr/right_active" />
<item android:state_pressed="true" android:state_enabled="false"
style="?attr/right_inactive" />
活动中:
TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab1", getResources().getDrawable(R.drawable.left)).setContent(new Intent(this, Left.class)));
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("Tab2", getResources().getDrawable(R.drawable.right))
.setContent(new Intent(this, Right.class)));
tabHost.setCurrentTab(1);
请帮忙...
I am using tab view in one of my activity. I want to change the drawable of the tabs on the basis of their selection. So it is like this -- I have 4 images T11, T12, T21, T22. I want to set the image T11 and T22 initially with tab 1 selected. Now I want to change the images to T12 and T21 as soon as I select Tab 2.
So far I tried using via an xml file of drawable type:
drawable for left tab( tab1) --
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
style="?attr/left_active" />
<item android:state_window_focused="false" android:state_enabled="false"
style="?attr/left_inactive" />
<item android:state_pressed="true" android:state_enabled="true"
style="?attr/left_active" />
<item android:state_pressed="true" android:state_enabled="false"
style="?attr/left_inactive" />
</selector>
Drawable for Tab right(Tab2) --
<item android:state_window_focused="false" android:state_enabled="true"
style="?attr/right_active" />
<item android:state_window_focused="false" android:state_enabled="false"
style="?attr/right_inactive" />
<item android:state_pressed="true" android:state_enabled="true"
style="?attr/right_active" />
<item android:state_pressed="true" android:state_enabled="false"
style="?attr/right_inactive" />
In activity:
TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab1", getResources().getDrawable(R.drawable.left)).setContent(new Intent(this, Left.class)));
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("Tab2", getResources().getDrawable(R.drawable.right))
.setContent(new Intent(this, Right.class)));
tabHost.setCurrentTab(1);
Please help...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我终于得到了我的问题的答案。我之前所做的就是正确的做法。我做错的是,在 drawable 文件中使用 style 属性。
因此,这里是供将来参考的示例代码:
可绘制文件(在可绘制文件夹中创建一个名为
tab_left.xml
的文件)将其设置为选项卡的背景图像:
I finally got the answer of my question. What I was doing earlier was the right approach. What I am doing wrong is, using style attribute in the drawable file.
So here is the example code for future references:
Drawable file ( create a file in drawable folder with name
tab_left.xml
)Setting this as background image of a tab:
我已经在我的应用程序中完成了此操作,但没有使用 xml/styles。我在代码中完成了它,然后在 onTabChanged() 方法中交换背景图像。
您可以在我的评论中看到部分代码 Android TabHost - 活动在每个选项卡内
onTabChanged 将如下所示:
I've done this in my app, but not using xml/styles. I did it in code and then swap the background images in the onTabChanged() method.
Part of the code you can see in my comment in post Android TabHost - Activities within each tab
The onTabChanged would then look like this: