首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >寻找一个通用的TabHost风格,将工作在安卓,宏达电感,三星等皮肤

寻找一个通用的TabHost风格,将工作在安卓,宏达电感,三星等皮肤
EN

Stack Overflow用户
提问于 2011-05-28 02:09:22
回答 2查看 1.8K关注 0票数 8

Android TabHost的默认样式适用于普通的Android系统。然而,在HTC Sense上,他们在黑色背景上使用黑色文本,这是不可读的。

让TabHost在各种不同风格的android皮肤上都能看到文本,最简单的方法是什么?如果可能的话,我不想做一个完全自定义的外观。

我的targetSDK是10,我的minSDK是7。

EN

回答 2

Stack Overflow用户

发布于 2011-08-31 04:21:16

我会给你一个建议,让你完全独立于TabWidget,创建你自己的导航,你可以随心所欲地定制它,而不是麻烦地使用僵硬的TabWidget。我并不是要丢弃这个令人敬畏的TabHost,因为它很容易使用带有自定义导航的TabHost:

首先将你的TabWidget设置为已不存在:

代码语言:javascript
复制
<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"/>

然后创建你自己的导航来代替它。您还可以创建一个菜单(使用硬件菜单按钮)或类似以下内容:

代码语言:javascript
复制
<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>

现在你在代码中要做的是:

代码语言:javascript
复制
@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中,你必须设置适当的活动,等等。你知道这些东西。

您可以使用任何东西进行导航,并且仍然可以使用TabHost的优点。别再为那个TabWidget操心了。;)

票数 4
EN

Stack Overflow用户

发布于 2011-09-03 23:03:13

经过一些研究,我认为最好的选择是使用ActionBar的选项卡式界面,因为它看起来更容易样式化。从Android1.6开始,ActionBarSherlock为ActionBar设备提供了兼容层。Here you can look at examples介绍了使用该库可以获得的内容。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6156116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档