代码之家  ›  专栏  ›  技术社区  ›  mudit

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

  •  2
  • mudit  · 技术社区  · 16 年前

    我正在我的某个活动中使用选项卡视图。我想根据选项卡的选择更改其可绘制性。它是这样的——我有4幅图像,t11,t12,t21,t22。我想设置图像t11和t22,首先选择标签1。现在,我想在选择选项卡2后立即将图像更改为t12和t21。

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

    左凸耳可牵引(表1)--

    <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>
    

    制表位右侧可拖动(制表位2)--

     <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);
    

    请帮助…

    2 回复  |  直到 11 年前
        1
  •  10
  •   Azhar Bandri    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);
    
        2
  •  1
  •   Community Mohan Dere    9 年前

    我已经在应用程序中完成了这项工作,但没有使用XML/样式。我在代码中做了这个操作,然后在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));
            }
        }