我在我的一个活动中使用标签视图。我想根据他们的选择更改选项卡的drawable。所以就像这样 - 我有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>
Draw for Tab右(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);
请帮忙......
答案 0 :(得分:10)
我终于得到了我的问题的答案。我之前做的是正确的做法。我错误的是,在 drawable 文件中使用 style 属性。
所以这是未来参考的示例代码:
可绘制文件(在名为tab_left.xml
的可绘制文件夹中创建文件)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false" android:state_selected="false"
android:state_pressed="false" android:drawable="@drawable/tab_left_inactive" />
<item android:state_focused="false" android:state_selected="true"
android:state_pressed="false" android:drawable="@drawable/tab_left_active" />
<item android:state_focused="true" android:state_selected="false"
android:state_pressed="false" android:drawable="@drawable/tab_left_inactive" />
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="false" android:drawable="@drawable/tab_left_active" />
<item android:state_pressed="true"
android:drawable="@drawable/tab_left_active" />
</selector>
将其设置为标签的背景图片:
TabWidget tw = getTabWidget();
View leftTabView = tw.getChildAt(0);
leftTabView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_left);
View rightTabView = tw.getChildAt(1);
rightTabView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_right);
答案 1 :(得分:1)
我已在我的应用中完成此操作,但未使用xml / styles。我在代码中完成了它,然后在onTabChanged()方法中交换背景图像。
您可以在帖子Android TabHost - Activities within each tab
中的评论中看到部分代码onTabChanged将如下所示:
public void onTabChanged(String tabId) {
if ("tabMap".equals(tabId)) {
txtTabMap.setBackgroundDrawable(getResources().getDrawable(newsList==null?R.drawable.bg_tab_right_active_left_inactive:R.drawable.bg_tab_middle_active_both_inactive));
txtTabInfo.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_tab_left_inactive_right_active));
if(txtTabNews!=null)txtTabNews.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_tab_right_inactive_left_active));
} else if ("tabInfo".equals(tabId)) {
scrlDescription.scrollTo(0, 0);
txtTabMap.setBackgroundDrawable(getResources().getDrawable(newsList==null?R.drawable.bg_tab_right_inactive_left_active:R.drawable.bg_tab_middle_inactive_left_active));
txtTabInfo.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_tab_left_active_right_inactive));
if(txtTabNews!=null)txtTabNews.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_tab_right_inactive_left_inactive));
} else if ("tabNews".equals(tabId)) {
txtTabMap.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_tab_middle_inactive_right_active));
txtTabInfo.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_tab_left_inactive_right_inactive));
if(txtTabNews!=null)txtTabNews.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_tab_right_active_left_inactive));
}
}