【问题标题】:Tabhost image change on selection选择时的 Tabhost 图像更改
【发布时间】:2014-03-25 12:43:04
【问题描述】:

我尝试使用图像中给出的背景颜色更改选项卡图像 这是我所做的来源...... 我也审查了这个 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>

这里是截图:

这是屏幕 2。

【问题讨论】:

标签: android tabs android-tabhost android-tabs


【解决方案1】:

您只需要为您的选项卡设置选择器。创建一个视图并使用您的选择器。

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

希望对你有所帮助..

【讨论】:

    【解决方案2】:

    第 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 步:

    为下面的自定义小部件使用可绘制文件夹。

    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>
    

    【讨论】:

    • 没有但和我提到的不一样......我试图在选择时在底部设置粉红色......当 false 想要白色背景但它显示为黑色...... @Sethu
    • 选择白色背景和粉红色两种形状或图像。如果选择意味着设置粉红色,则表示设置足够的白色图像。
    • 我做到了,但没有发生...@Sethu
    • 它为我工作.. 所以把它写成你的代码我会指导你。
    • 好的...现在看看我的代码...在我的eclipse中添加了两个xml...@Sethu
    猜你喜欢
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多