【问题标题】:How to add programmatically generated gallery to tab layout?如何以编程方式生成的画廊添加到选项卡布局?
【发布时间】:2012-04-30 21:51:34
【问题描述】:

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

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 的视图

有什么帮助吗?

提前谢谢你。

【问题讨论】:

    标签: android android-layout tabs


    【解决方案1】:

    你应该看看TabContentFactory

    您的解决方案是实现TabContentFactory 并在createTabContent 方法中返回Gallery 实例并在addTab 中使用setContent(TabHost.TabContentFactory contentFactory) 方法。

    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));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-20
      • 2016-12-30
      • 1970-01-01
      • 2019-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-24
      相关资源
      最近更新 更多