如何添加程序生成的画廊标签布局? [英] How to add programmatically generated gallery to tab layout?

查看:132
本文介绍了如何添加程序生成的画廊标签布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写信给在运行时生成库的方法。

I write a method to generate gallery at run time.

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;

}

然后,我创建了一个厨房,并尝试将其设置为一个标签布局。

Then I create a galley and try to set it to a Tab Layout.

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

Could not create tab content because could not find view with id 2130968576

任何帮助?

感谢您提前。

推荐答案

您应该看一看的 TabContentFactory

您的解决方案是实施 TabContentFactory ,并在图库实例> createTabContent 方法和使用 setContent(TabHost.TabContentFactory contentFactory)方法 addTab

The solution for you would be to implement TabContentFactory and return the Gallery instance in the createTabContent method and use setContent(TabHost.TabContentFactory contentFactory) method in 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));

这篇关于如何添加程序生成的画廊标签布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆