寻找一种适用于Android,HTC Sense,Samsung等皮肤的通用TabHost样式

问题描述

| Android TabHost的默认样式适用于直接的Android系统。但是,在HTC Sense上,它们在深色背景上使用深色文本,这是不可读的。 拥有一个TabHost的最简单方法是什么,该TabHost可以在所有不同种类的android skin中显示可见文本?如果可能,我希望不必进行完全自定义的外观。 我的targetSDK是10,而我的minSDK是7。     

解决方法

        我会建议您完全独立于TabWidget,并创建自己的导航,您可以根据自己的喜好自定义导航,而不必担心僵硬的TabWidget。我并不是要放弃很棒的TabHost,因为使用TabHost和自定义导航很容易: 首先将TabWidget设置为不存在:
<TabHost
    xmlns:android=\"http://schemas.android.com/apk/res/android\"
    android:id=\"@android:id/tabhost\"
    android:layout_width=\"fill_parent\"
    android:layout_height=\"fill_parent\">
    <RelativeLayout
        android:layout_width=\"fill_parent\"
        android:layout_height=\"fill_parent\">
        <TabWidget
            android:id=\"@android:id/tabs\"
            android:layout_width=\"fill_parent\"
            android:layout_height=\"wrap_content\"
            android:visibility=\"gone\"/>
然后代替它创建您自己的导航。您也可以创建一个菜单(使用硬件菜单按钮)或类似以下的内容:
<TabHost
    xmlns:android=\"http://schemas.android.com/apk/res/android\"
    android:id=\"@android:id/tabhost\"
    android:layout_width=\"fill_parent\"
    android:layout_height=\"fill_parent\">
    <RelativeLayout
        android:layout_width=\"fill_parent\"
        android:layout_height=\"fill_parent\">
        <TabWidget
            android:id=\"@android:id/tabs\"
            android:layout_width=\"fill_parent\"
            android:layout_height=\"wrap_content\"
            android:visibility=\"gone\"/>
        <!-- content of your tabs-->
        <FrameLayout
            android:id=\"@android:id/tabcontent\"
            android:layout_width=\"fill_parent\"
            android:layout_height=\"fill_parent\"
            android:layout_above=\"@id/slider_stub\"
            android:background=\"@color/overview_banner_bg_down\"/>
        <!-- custom slider with horizontal scrollable radio buttons-->
        <com.example.WrappingSlidingDrawer
            android:id=\"@+id/tab_slider\"
            android:layout_width=\"fill_parent\"
            android:layout_height=\"wrap_content\"
            android:layout_alignParentBottom=\"true\"
            android:handle=\"@+id/tab_slider_handle\"
            android:content=\"@+id/tab_scroller\">
            <RelativeLayout
                android:id=\"@+id/tab_slider_handle\"
                android:layout_width=\"fill_parent\"
                android:layout_height=\"wrap_content\"
                android:background=\"@drawable/def_slider\">
            </RelativeLayout>
            <HorizontalScrollView
                android:id=\"@+id/tab_scroller\"
                android:layout_width=\"fill_parent\"
                android:layout_height=\"wrap_content\"
                android:fadeScrollbars=\"false\"
                android:scrollbarAlwaysDrawHorizontalTrack=\"true\"
                android:scrollbarTrackHorizontal=\"@drawable/scrollbar_horizontal_track\"
                android:scrollbarThumbHorizontal=\"@drawable/scrollbar_horizontal_thumb\"
                android:fillViewport=\"true\"
                android:scrollbarSize=\"3dip\"
                android:fadingEdgeLength=\"80dp\"
                android:background=\"#343534\">
                <RelativeLayout
                    android:layout_width=\"fill_parent\"
                    android:layout_height=\"wrap_content\">
                    <RadioGroup
                        android:id=\"@+id/custom_tabs\"
                        android:layout_width=\"fill_parent\"
                        android:layout_height=\"wrap_content\"
                        android:layout_centerHorizontal=\"true\"
                        android:gravity=\"center\"
                        android:orientation=\"horizontal\"
                        android:checkedButton=\"@+id/tab_overview\">
                        <RadioButton
                            android:id=\"@+id/tab_overview\"
                            android:layout_height=\"55dp\"
                            android:layout_width=\"55dp\"
                            android:button=\"@null\"
                            android:background=\"@drawable/def_checktab_overview\"/>
                        <RadioButton
                            android:id=\"@+id/tab_news\"
                            android:layout_height=\"55dp\"
                            android:layout_width=\"55dp\"
                            android:button=\"@null\"
                            android:background=\"@drawable/def_checktab_news\"/>
                            .....etc....your tabs......
                    </RadioGroup>
                </RelativeLayout>
            </HorizontalScrollView>
        </com.example.WrappingSlidingDrawer>
    </RelativeLayout>
</TabHost>
您现在必须在代码中做什么:
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_tab_layout);

    setTabs();

    RadioGroup tabs = (RadioGroup) findViewById(R.id.custom_tabs);

    tabs.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group,int checkedId) {
            switch (checkedId) {
            case R.id.tab_overview:
                getTabHost().setCurrentTab(0);
                break;
            case R.id.tab_news:
                getTabHost().setCurrentTab(1);
                break;
            ....etc.....
            }
        }
    });
}

/**
 * Delegate tab creation and adding.
 */
private void setTabs() {
    // add the necessary tabs
    addTab(R.string.tab_overv_tag,OverviewActivityGroup.class);
    addTab(R.string.tab_news_tag,NewsActivityGroup.class);
            .....etc.....
}

/**
 * Create a tab with an Activity and add it to the TabHost
 *  
 * @param tagId
 *            resource id of the string representing the tag for finding the tab    
 * @param activity
 *            the activity to be added
 */
private void addTab(int tagId,Class<? extends ActivityGroup> activity) {
    // create an Intent to launch an Activity for the tab (to be reused)
    Intent intent = new Intent().setClass(this,activity);
    // initialize a TabSpec for each tab and add it to the TabHost
    TabHost.TabSpec spec = usedTabHost.newTabSpec(getString(tagId));
    // use layout inflater to get a view of the tab to be added
    View tabIndicator = getLayoutInflater().inflate(R.layout.tab_indicator,getTabWidget(),false);
    spec.setIndicator(tabIndicator);
    spec.setContent(intent);
    usedTabHost.addTab(spec);
}
不需要整个指标东西。^^在您的ActivityGroups中,您必须设置适当的Activity等。您知道这些东西。 您可以使用任何东西进行导航,并且仍然可以使用TabHost的优点。只是不要再理会那个TabWidget了。 ;)     ,        经过研究后,我认为最好的选择是使用ActionBar的选项卡式界面,因为它看起来更易于样式设置。 ActionBarSherlock为从Android 1.6开始的设备提供ActionBar兼容性层。在这里,您可以查看有关如何使用该库的示例。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...