0

我编写了一个在运行时生成图库的方法。

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定义为此处提及的内容。

我得到一个例外如下。

无法创建选项卡内容,因为找不到 ID 为 2130968576 的视图

对此有什么帮助吗?

先感谢您。

4

1 回答 1

1

你应该看看TabContentFactory

您的解决方案是在方法中实现TabContentFactory并返回Gallery实例并在.createTabContentsetContent(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));
于 2012-04-19T12:04:05.980 回答