如何更改Android中的标签样式?
答
您可以通过代码调整标签 - 这里是我的应用程序摘录,但您也可以直接指定主题而不是背景图片。 (我还没有通过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);
...
}
+0
规范。 setIndicator(txtTabInfo);这是不允许的。编译器在此行发生错误。 – sachin 2010-09-03 08:44:02
答
您也可以通过XML创建并为您的应用中的所有选项卡创建/定义全局主题。有一个在这里创建控件主题的更多信息:http://developer.android.com/guide/topics/ui/themes.html
答
我用我的绘制如下定义,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);
最优雅的解决方案,我看到到现在为止是:http://stackoverflow.com/questions/3078081/setting-a-global-style-for-views-in-android/3166865#3166865 – 2011-09-02 17:43:52