Android的TabHost使用笔记
- Activity继承自TabAcitvity的使用,目前TabActivity不推荐使用,官方已经不建议使用了
public class MainTabActivity extends TabActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TabHost tabHost = getTabHost(); LayoutInflater.from(this).inflate(R.layout.tab1, tabHost.getTabContentView(), true); LayoutInflater.from(this).inflate(R.layout.tab2, tabHost.getTabContentView(), true); tabHost.addTab(tabHost.newTabSpec("TAB1").setIndicator("线性布局") .setContent(R.id.layout01)); tabHost.addTab(tabHost.newTabSpec("TAB2").setIndicator("相对布局") .setContent(R.id.layout03)); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main_tab, menu); return true; } }
- xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainTabActivity" > </RelativeLayout>
- Activity直接使用TabHost,这种比较常用,不过使用过程中会出现很多问题
先上正确的使用代码:
@SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // //ListView // ListView ls=(ListView) findViewById(R.id.listView1); // ls.setAdapter(new SimpleAdapter(this,getData(),R.layout.goods, // new String[]{"title","info","img"},new // int[]{R.id.title,R.id.info,R.id.imageView1} // ) ); // set Adapter SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.goods, new String[] { "title", "info", "img" }, new int[] { R.id.title, R.id.info, R.id.imageView1 }); setListAdapter(adapter); setContentView(R.layout.activity_main); if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() .permitAll().build(); StrictMode.setThreadPolicy(policy); } // data // 获取TabHost TabHost tabHost = (TabHost) findViewById(R.id.tabs); tabHost.setup(); LayoutInflater.from(this).inflate(R.layout.hometab, tabHost.getTabContentView(), true); LayoutInflater.from(this).inflate(R.layout.categorytab, tabHost.getTabContentView(), true); tabHost.addTab(tabHost.newTabSpec("parent").setIndicator("首页",this.getResources().getDrawable(R.drawable.home_basic)) .setContent(R.id.hometab)); tabHost.addTab(tabHost.newTabSpec("catetory").setIndicator("分类",this.getResources().getDrawable(R.drawable.book_bookmark)) .setContent(R.id.catetorytab)); }
xml代码:
<TabHost android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" > </TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> </FrameLayout> </LinearLayout> </TabHost>
[/java]
常见问题:
1.TabHost的Tab内容显示其他xml页面进行显示,结果提示id找不到,
错误提示:Could not create tab content because could not find view with id
原因:直接使用
TabSpec homeSpec = tabHost.newTabSpec("parent"); homeSpec.setIndicator("首页", this.getResources().getDrawable(R.drawable.home_basic)); homeSpec.setContent(R.id.catetorytab);//这样赋值xml的视图ID,不能获取到值,需要先用LayoutInflater进行xml文件的解析并赋值给tabhost
代码如下:
tabHost.Setup();//注意必须在设置之下写 LayoutInflater.from(this).inflate(R.layout.categorytab, tabHost.getTabContentView(), true);