【问题标题】:How to change tab style in Android?如何在 Android 中更改标签样式?
【发布时间】:2011-03-03 00:38:05
【问题描述】:

我希望我的 Android 标签看起来像官方 TWitter 应用中的一样扁平和简单。如何使用样式/主题定义覆盖默认(浅色)主题并更改选项卡的背景图像?

【问题讨论】:

标签: android coding-style tabs themes


【解决方案1】:

我对我的可绘制对象使用了以下定义,tab_background.xml

   <selector xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:state_selected="true" android:drawable="@drawable/tab_bg_selected" />
        <item android:drawable="@drawable/tab_bg_normal" /> 
   </selector>

然后遍历选项卡以设置它们。

TabWidget tabWidget = tabHost.getTabWidget();

for(int i=0; i<tabWidget.getChildCount(); i++)
  tabWidget.getChildAt(i).setBackgroundResource(R.drawable.tab_background);

【讨论】:

    【解决方案2】:

    您也可以通过 XML 来实现,并为应用中的所有选项卡创建/定义一个全局主题。这里有更多关于创建小部件主题的信息:http://developer.android.com/guide/topics/ui/themes.html

    【讨论】:

      【解决方案3】:

      您可以通过代码调整选项卡 - 这是我的应用程序的摘录,但您也可以直接分配主题而不是背景图像。 (我还没有使用通过xml属性的方法,不确定是否也可以使用)。

      private void initTabs() {
          tabs = (TabHost) findViewById(R.id.tabhost);
          tabs.setup();
          tabs.setBackgroundResource(R.drawable.bg_midgray);
      
         TabHost.TabSpec spec;
      
         // Location info
         txtTabInfo = new TextView(this);
         txtTabInfo.setText("INFO");
         txtTabInfo.setPadding(0, 5, 0, 0);
         txtTabInfo.setTextSize(11);
      
         txtTabInfo.setBackgroundResource(R.drawable.bg_tab_left_active_right_inactive);
         txtTabInfo.setTextColor(Color.DKGRAY);
         txtTabInfo.setGravity(Gravity.CENTER_HORIZONTAL);
         txtTabInfo.setHeight(39);
         spec = tabs.newTabSpec("tabInfo");
         spec.setContent(R.id.tabInfo);
         spec.setIndicator(txtTabInfo);
         tabs.addTab(spec);
         ...
      }
      

      【讨论】:

      • spec.setIndicator(txtTabInfo);这是不允许的。编译器在这一行给出错误。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-04
      • 1970-01-01
      • 2020-06-29
      • 1970-01-01
      • 2021-03-07
      • 2018-02-07
      相关资源
      最近更新 更多