【问题标题】:How to overlap views in RelativeLayout, dynamically?如何动态重叠RelativeLayout中的视图?
【发布时间】:2012-03-21 14:25:37
【问题描述】:

我在 RelativeLayout 中插入了一个 ImageView。在这个 ImageView 之上,我试图插入一个进度条,下载图像后它会变得不可见。但是,当我在添加 ImageView 后添加进度条时,它给了我一个错误 -

java.lang.IllegalStateException:指定的孩子已经有一个父母。您必须先在孩子的父母上调用 removeView()。

代码如下:

                mRelativeLayout = (RelativeLayout) mGallery.findViewById(R.id.relative_progress_spin_layout);
                RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
                relativeLayoutParams.addRule(RelativeLayout.ALIGN_TOP, R.id.progress_spin);

                progressBar = (ProgressBar) mGallery.findViewById(R.id.progress_spin);


                image = new ImageView(GalleryModuleActivity.this);
                image.setPadding(4, 4, 4, 4);
                image.setScaleType(ImageView.ScaleType.FIT_XY);

                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(200, LinearLayout.LayoutParams.MATCH_PARENT);
                image.setLayoutParams(layoutParams);

                mRelativeLayout.addView(image);
                mRelativeLayout.addView(progressBar);
                mHorizontalLayout.addView(mRelativeLayout);

谢谢..

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    你已经在布局中有ProgressBar(你用findViewById搜索它)所以你不应该再次将它添加到布局中(与mRelativeLayout@987654323相同的东西@如果它已经在布局文件中)。删除此行:

    mRelativeLayout.addView(progressBar);
    mHorizontalLayout.addView(mRelativeLayout);
    

    如果布局中有视图,则不要将它们再次添加到布局中!

    【讨论】:

      【解决方案2】:

      您究竟想对图像和进度条做什么。如果要在图像上显示 ProgressBar,请使用 FrameLayout。在那里你也可以使用 VISIBLE 和 GONE 的东西。

      您想在哪里显示动态生成的视图? 因为我们确实有适配器来显示具有不同内容的相同类型的数据。

      【讨论】:

        猜你喜欢
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多