Tabhost图像在选择时更改

时间:2014-03-25 12:43:04

标签: android tabs android-tabhost android-tabs

我尝试使用图片中给出的背景颜色更改标签图像 这是我所做的来源......  我也审查过这个 How to change the Tabs Images in the TabHost

问题出在哪里?

       mTabHst.addTab(mTabHst.newTabSpec("tab_test1").setIndicator(null,res.getDrawable(R.drawable.custom_widget_list))
                .setContent(i));   

     mTabHst.addTab(mTabHst.newTabSpec("tab_test2").setIndicator(null,res.getDrawable(R.drawable.custom_widget_trans))
                .setContent(j));


         int tabCount = mTabHst.getTabWidget().getTabCount();
         for (int r = 0; r < tabCount; r++) {
             final View view = mTabHst.getTabWidget().getChildTabViewAt(r);

             if ( view != null ) {
                 // reduce height of the tab
                 view.getLayoutParams().height *= 0.70;

             }
         }


         mTabHst.setCurrentTab(0); 

}

这是我的标签xml

 <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" 
        android:layout_gravity="center"
        android:background="#FFFFFF"
        android:tabStripEnabled="false" 
        android:gravity="center"  />

custom_widget_list.xml

<item android:drawable="@drawable/member_pink" android:state_pressed="true"   android:state_selected="false" android:color="#FFFFFF"></item>
<item android:drawable="@drawable/username" android:state_pressed="false" android:state_selected="false" android:color="#FFFFFF"></item>
<item android:drawable="@drawable/member_pink" android:state_pressed="false" android:state_selected="true" android:color="#FF00FF"></item>

custom_widget_trans.xml

    <?xml version="1.0" encoding="utf-8"?>
   <selector xmlns:android="http://schemas.android.com/apk/res/android" > 
<item android:drawable="@drawable/member_pink" android:state_pressed="true"      android:state_selected="false" android:color="#FFFFFF"></item>
   <item android:drawable="@drawable/member4" android:state_pressed="false"   android:state_selected="false" android:color="#FFFFFF"></item>
    <item android:drawable="@drawable/member_pink" android:state_pressed="false"  android:state_selected="true" android:color="#FF00FF"></item>

这是截图:

enter image description here

这是屏幕2。

enter image description here

2 个答案:

答案 0 :(得分:3)

您只需为标签设置选择器即可。创建一个视图并使用您的选择器。

第1步:在您的创建方法中,您将使用选择器背景返回视图。

private View getTabIndicator(){
    View view = new View(this);
    view.setBackgroundResource(R.drawable.tab_selector);
    return view;
}

第2步:将tab_selector.xml保存在res / drawable文件夹中。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--  Active tab -->
<item android:state_selected="true" android:state_focused="false"
    android:state_pressed="false" android:drawable="@drawable/tab_bg_selected" />
<!--  Inactive tab -->
<item android:state_selected="false" android:state_focused="false"
android:state_pressed="false" android:drawable="@drawable/tab_bg_unselected" />
<!--  Pressed tab -->
<item android:state_pressed="true" android:drawable="@android:color/transparent" />
<!--  Selected tab (using d-pad) -->
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="false" android:drawable="@android:color/transparent" />
</selector>

第3步:现在,setIndicator()将自定义视图作为指标,选择器将管理背景图像。

mTabHst.addTab(mTabHst.newTabSpec("tab_test2").setIndicator(getTabIndicator()).setContent(j));

希望它会帮助你..

答案 1 :(得分:0)

第1步:

将以下代码添加到oncreate方法中。

    TabSpec Summary = tabHost.newTabSpec("Study  Summary");
        Summary.setIndicator(NetworkStatus.createTabView(
                TabActivityPacs.activity, R.drawable.custom_widget_list));
        Intent studysummary = new Intent(this, Stusummaryactivity.class);
        Summary.setContent(studysummary);

        TabSpec trans= tabHost.newTabSpec("Transcription");
        trans.setIndicator(NetworkStatus.createTabView(
                TabActivityPacs.activity, R.drawable.custom_widget_trans));
        Intent trintent = new Intent(this, Testtranscription.class);
        trans.setContent(trintent);

             tabHost.addTab(StudySummary);
    tabHost.addTab(Transcription);

第2步:

将drawable文件夹用于下面的自定义小部件。

custom_widget_list.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

   <!-- Tab widget design -->
    <item android:drawable="@drawable/listpres" android:state_pressed="true"   android:state_selected="false"></item>
    <item android:drawable="@drawable/list" android:state_pressed="false" android:state_selected="false"></item>
    <item android:drawable="@drawable/listpres" android:state_selected="true"></item>

</selector>

custom_widget_trans.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" ><!-- Tab widget design -->
    <item android:drawable="@drawable/transcription1pres" android:state_pressed="true" android:state_selected="false"></item>
    <item android:drawable="@drawable/transcription" android:state_pressed="false" android:state_selected="false"></item>
    <item android:drawable="@drawable/transcription1pres" android:state_selected="true"></item>

</selector>