我想做一个简单的标签应用,这个标签应用中有两个选项卡。现在的问题是当我在标签应用中加入下面的代码,只显示文本,没有图标。如果我给文本加 "",图标就会显示出来。我用的android版本是4.0.3。
如何修改代码,让图标显示出来呢?
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TabWidget android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs" />
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabcontent" >
<LinearLayout android:id="@+id/tab1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="@+id/textView1"
android:text="Contenido Tab 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:id="@+id/tab2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="@+id/textView2"
android:text="Contenido Tab 2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
activity 代码:
public class TabTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
TabHost tabs=(TabHost)findViewById(R.id.tabhost);
tabs.setup();
TabHost.TabSpec spec=tabs.newTabSpec("mitab1");
spec.setContent(R.id.tab1);
spec.setIndicator("sss",
res.getDrawable(android.R.drawable.ic_btn_speak_now));
tabs.addTab(spec);
spec=tabs.newTabSpec("mitab2");
spec.setContent(R.id.tab2);
spec.setIndicator("TAB2",
res.getDrawable(android.R.drawable.ic_dialog_map));
tabs.addTab(spec);
tabs.setCurrentTab(0);
}
当我添加spec.setIndicator("", res.getDrawable(android.R.drawable.ic_dialog_map));
,可以看见图标,但是当我添加spec.setIndicator("TAB2", res.getDrawable(android.R.drawable.ic_dialog_map));
,我只能看见 TAB2 ,不能看见另一个,看似没有足够的空间来显示这两个选项卡,所以我又添加tabs.getTabWidget().getChildAt(1).getLayoutParams().height = 150;
来增加选项卡的高度,还是没有管用。
你只加载第一个,所以只能看见最新添加的那个。
TabHost.TabSpec spec=tabs.newTabSpec("mitab1");
spec.setIndicator("sss",
res.getDrawable(android.R.drawable.ic_btn_speak_now));
Intent sssIntent = new Intent(this, First.class);
spec.setContent(sssIntent);
tabs.addTab(spec);
TabHost.TabSpec spec2=tabs.newTabSpec("mitab2");
spec2=tabs.newTabSpec("mitab2");
spec2.setIndicator("TAB2",
res.getDrawable(android.R.drawable.ic_dialog_map));
Intent sssIntent2 = new Intent(this, Second.class);
spec2.setContent(sssIntent2 );
tabs.addTab(spec2);
我用空值替换了标签名,现在你只能看见图标。
TabHost.TabSpec spec=tabs.newTabSpec("mitab1");
spec.setIndicator("",
res.getDrawable(android.R.drawable.ic_btn_speak_now));
Intent sssIntent = new Intent(this, First.class);
spec.setContent(sssIntent);
tabs.addTab(spec);
解决了,安卓4.0会有这个问题,修改Manifest.xml里面的Theme,找到System Resources,里面有Theme.black,选这个就行了。
剩下自己要改背景色什么的这个还是比较easy的吧~