2

我在我的一项活动中使用标签视图。我想根据选项卡的选择更改选项卡的可绘制性。就像这样——我有 4 张图像 T11、T12、T21、T22。我想最初选择选项卡 1 来设置图像 T11 和 T22。现在我想在选择 Tab 2 后立即将图像更改为 T12 和 T21。

到目前为止,我尝试通过可绘制类型的 xml 文件使用:

左标签(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>

右标签(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);

请帮忙...

4

2 回答 2

11

我终于得到了我的问题的答案。我之前做的是正确的方法。我做错的是,在可绘制文件中使用样式属性。

所以这里是供将来参考的示例代码:

可绘制文件(在可绘制文件夹中创建一个具有名称的文件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);
于 2010-07-09T06:24:33.537 回答
1

我已经在我的应用程序中完成了这项工作,但没有使用 xml/styles。我在代码中完成了它,然后在 onTabChanged() 方法中交换了背景图像。

您可以在我的评论中看到部分代码Android TabHost - 每个选项卡内的活动

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));
        }
    }
于 2010-07-08T06:30:33.957 回答