我编写了一个在运行时生成库的方法。
private Gallery createGallery(ImageAdapter imageAdapter) {
Gallery sampleGallery = new Gallery(getApplicationContext());
sampleGallery.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
sampleGallery.setGravity(Gravity.FILL_VERTICAL);
sampleGallery.setSpacing(5);
sampleGallery.setAdapter(imageAdapter);
sampleGallery.setSelection(1);
return sampleGallery;
}
然后我创建一个厨房并尝试将其设置为标签布局。
final TabHost mTabHost = (TabHost) findViewById(R.id.tabHost);
mTabHost.setup();
Gallery tabGallery = createGallery(brkingNewAdapter); // my image adapter
tabGallery.setId(R.id.gallery_1);
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1")
.setContent(R.id.gallery_1));
R.id.gallery_1 在here中被定义为提及。
我得到如下例外。
无法创建标签内容,因为无法找到ID为2130968576的视图
对此有何帮助?
提前谢谢。
答案 0 :(得分:1)
你应该看看TabContentFactory。
您的解决方案是实施TabContentFactory
并在Gallery
方法中返回createTabContent
个实例,并在setContent(TabHost.TabContentFactory contentFactory)
中使用addTab
方法。
class GalleryContentFactory implements TabContentFactory{
private ImageAdapter imageAdapter;
public GalleryContentFactory(ImageAdapter imageAdapter){
this.imageAdapter = imageAdapter;
}
public View createTabContent(String tag){
Gallery sampleGallery = new Gallery(getApplicationContext());
sampleGallery.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
sampleGallery.setGravity(Gravity.FILL_VERTICAL);
sampleGallery.setSpacing(5);
sampleGallery.setAdapter(imageAdapter);
sampleGallery.setSelection(1);
return sampleGallery;
}
}
并添加到标签:
GalleryContentFactory galleryFactory = new GalleryContentFactory(brkingNewAdapter);
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2")
.setContent(galleryFactory));