代码之家  ›  专栏  ›  技术社区  ›  David Hedlund

轻主题的Android TabWidget

  •  6
  • David Hedlund  · 技术社区  · 16 年前

    我有一个针对1.5框架的应用程序,它使用默认的灯光主题。当使用带有此主题的选项卡小部件时,选项卡图像几乎不可见,并且除了当前活动的选项卡之外,选项卡标题很难识别。

    在默认的黑暗主题中,这些标签非常清晰,但这不是一个我很满意的解决方案。有没有一个简单的设置,我可以设置选项卡小部件,以便在灯光主题中更好地可见性,或者我必须手动修改图像和文本样式?

    4 回复  |  直到 15 年前
        1
  •  10
  •   yanokwa    15 年前

    它不漂亮,但你可以在你的标签活动中尝试。

    // light theme support
    final TabHost tabHost = getTabHost();
    tabHost.setBackgroundColor(Color.WHITE);
    tabHost.getTabWidget().setBackgroundColor(Color.BLACK);
    
    // hack to set font size
    LinearLayout ll = (LinearLayout) tabHost.getChildAt(0);
    TabWidget tw = (TabWidget) ll.getChildAt(0);
    
    // first tab
    RelativeLayout rllf = (RelativeLayout) tw.getChildAt(0);
    lf = (TextView) rllf.getChildAt(1);
    lf.setTextSize(21);
    lf.setPadding(0, 0, 0, 6);
    
    // second tab
    RelativeLayout rlrf = (RelativeLayout) tw.getChildAt(1);
    rf = (TextView) rlrf.getChildAt(1);
    rf.setTextSize(21);
    rf.setPadding(0, 0, 0, 6);
    

    /res/values/colors.xml应该

    <resources>
        <drawable name="black">#ff000000</drawable>
        <drawable name="white">#ffffffff</drawable>
    </resources>
    

    androidmaniest.xml应该

    <application android:theme="@android:style/Theme.Light">
    

    如果你想做更疯狂的事,试试看 http://ezmobile.wordpress.com/2009/02/02/customized-android-tabs/

        2
  •  2
  •   Roman Nurik    16 年前

    这是一个错误,你能在 the issue tracker ?

    Afaik,您定制文本和图像样式的解决方案听起来不错。

    值得注意的是,2.0中的选项卡小部件似乎没有轻量级风格。

        3
  •  2
  •   Brady Kroupa    15 年前

    通过使用HierarchyViewer工具,我在选项卡中找到了文本视图的Android ID。更改文本属性(包括颜色)的更好方法是执行以下操作…

    TabWidget tw = (TabWidget)tabHost.findViewById(android.R.id.tabs);
    View tabView = tw.getChildTabViewAt(0);
    TextView tv = (TextView)tabView.findViewById(android.R.id.title);
    tv.setTextSize(20);
    
        4
  •  2
  •   Lucy    15 年前

    解决布局中颜色/对比度问题的非常简单的方法:

    <TabWidget
       android:id="@android:id/tabs"
       android:background="#FF000000"
       android:padding="2dp"
    

    这会将TabWidget的背景设置为黑色,并添加一些填充,以便与黑色背景下的选项卡形成对比。它不是完美的,但是在1.5,2.2,明暗主题中工作。