更改所选选项卡的可绘制性--android

更改所选选项卡的可绘制性--android,android,tabs,drawable,Android,Tabs,Drawable,我正在我的一个活动中使用选项卡视图。我想根据选项卡的选择更改选项卡的可绘制性。它是这样的-,我有4幅图像,T11,T12,T21,T22。我想先设置图像T11和T22,并选择tab 1。现在我想在选择Tab 2后立即将图像更改为T12和T21 到目前为止,我尝试通过可绘制类型的xml文件使用: 左凸耳可拉伸(表1)-- 请帮助…我已经在我的应用程序中这样做了,但没有使用xml/样式。我在代码中这样做,然后在onTabChanged()方法中交换背景图像 您可以在我的帖子评论中看到部分代码 然后,

我正在我的一个活动中使用选项卡视图。我想根据选项卡的选择更改选项卡的可绘制性。它是这样的-,我有4幅图像,T11,T12,T21,T22。我想先设置图像T11和T22,并选择tab 1。现在我想在选择Tab 2后立即将图像更改为T12和T21

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

左凸耳可拉伸(表1)--


请帮助…

我已经在我的应用程序中这样做了,但没有使用xml/样式。我在代码中这样做,然后在onTabChanged()方法中交换背景图像

您可以在我的帖子评论中看到部分代码

然后,更改后的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));
        }
    }

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

下面是供将来参考的示例代码:

可绘制文件(在可绘制文件夹中创建一个名为
tab\u left.xml的文件)


谢谢Mathias但我还有一个问题。。我在我的应用程序中使用了主题,因此一个选项卡将有多个图像,如果用户更改应用程序的主题,这些图像将发生变化。那么我是如何实现这一点的呢?这正是我一直在寻找的,就像一个魔咒,非常容易实现。谢谢当我的代码帮助别人的时候,我总是觉得很高兴。。。若投票按钮对你们有帮助的话,你们可以按下它。这对解决我的问题真的很有帮助。感谢在style=?@jonney上问号符号代表什么?问号表示它是对当前主题中资源值的引用。
 <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);
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));
        }
    }
<?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);