[转载]自定义TabHost控件 – _Android_红颜 – eoe移动开发者社区.
今天学习了一下 TabHost以前也是一知半解 今天想系统了解下 JAVA代码非常简单 xml只是2个简单布局 这里就不列出了
TabHost tabHost = (TabHost)findViewById(R.id.tabHost);
tabHost.setup();
LayoutInflater inflater = LayoutInflater.from(this);
inflater.inflate(R.layout.tab1, tabHost.getTabContentView());
inflater.inflate(R.layout.tab2, tabHost.getTabContentView());
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("TAB1").setContent(R.id.tab1));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("TAB2").setContent(R.id.tab2));
学习过程中遇到了这样的错误 在setContent里 我添入的是 layout的ID 导致运行出错
Could not create tab content because could not find view with id 2130903041
这里是我的不仔细 查了很久发现原来 这里serContent()里边的要是视图的id,比如
LinearLayout的ID 这样的,结果把2个xml文件里的Layout加上id再载入就OK了,这里要
反思一下 为什么会出现这种细节错误
底部标签的实现:
其实也很简单,就是xml里改一下 2个属性
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
这里FrameLayout要写在上边 weight要设为1 然后还要在外边加一层垂直布局的LinearLayout
Mikel