更改选择的标签的drawable - android

mud*_*dit 2 tabs android drawable

我在我的一个活动中使用选项卡视图.我想根据他们的选择更改选项卡的drawable.所以就像这样 - 我有4张图像T11,T12,T21,T22.我想先选择标签1来设置图像T11和T22.现在我想在选择Tab 2后立即将图像更改为T12和T21.

到目前为止,我尝试使用drawable类型的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>
Run Code Online (Sandbox Code Playgroud)

可选项目标签(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" />
Run Code Online (Sandbox Code Playgroud)

在活动中:

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);
Run Code Online (Sandbox Code Playgroud)

请帮忙...

mud*_*dit 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>
Run Code Online (Sandbox Code Playgroud)

将其设置为选项卡的背景图像:

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);
Run Code Online (Sandbox Code Playgroud)