+-
android – WebView在第二次初始化后显示为纯白框
编辑:tl;博士:WebView显示为白色框,即使我似乎正确设置它,实际上它确实在前两次工作,但随后失败)

编辑:Video showing the problem in action…

我有以下一些代码从xml中扩展了一个视图(包含一个WebView),它定义了它:

private void createCard(ViewGroup cvFrame, Card card) {

    //... setup vairables...

    cvFrame.clearDisappearingChildren();
    cvFrame.clearAnimation();


    try {
        View cv = LayoutInflater.from(getBaseContext()).inflate(R.layout.card_back_view,
                cvFrame, true);
        cv.setBackgroundDrawable(Drawable.createFromStream(mngr.open(deckName + "_Card_back.png"), deckName));

        TextView suit = (TextView)cv.findViewWithTag("card_back_suit");
        //...setup text view for suit, this code works fine every time...

        WebView title = (WebView)cv.findViewWithTag("card_back_title");
        //This WebView doesn't appear to be the one which actually appears on screen (I can change settings till I'm blue in the face, with no effect)
        if (title != null) {
            title.setBackgroundColor(0x00000000);
            title.loadData(titleText, "text/html", "UTF-8");
        } else {
            Log.e("CardView", "Error can't find title WebView");
        }
    } catch (IOException e) {
        Log.e("CardView", "Error making cards: ", e);
    }
}

当我的Activity中的onCreate方法调用此方法时,WebView包含正确的代码,并且适当透明.

我有一个手势监听器,它用不同的内容替换ViewGroup的内容(它将顶部卡片设置为左侧,用卡片2替换顶部卡片的内容,将顶部卡片放回,然后用卡片3替换卡片2) )

//Gesture listener event
ViewGroup cvFrame = (ViewGroup)findViewById(R.id.firstCard);
cardLoc++
cvFrame.startAnimation(slideLeft);

(onAnimationEnd代码)

public void onAnimationEnd(Animation animation) {
    if (animation == slideLeft) {
        ViewGroup cvFrameOldFront = (ViewGroup)findViewById(R.id.firstCard);
        ViewGroup cvFrameNewFront = (ViewGroup)findViewById(R.id.secondCard);

        createCard(cvFrameOldFront, cards.get((cardLoc)%cards.size()));
        createCard(cvFrameNewFront, cards.get((cardLoc+1)%cards.size()));

        TranslateAnimation slideBack = new TranslateAnimation(0,0,0,0);
        slideBack.setDuration(1);
        slideBack.setFillAfter(true);
        cvFrameOldFront.startAnimation(slideBack);

    } 
}

当动画发生并且我替换了卡片的内容时,TextView套装被替换得很好,代码肯定会通过代码来替换WebView内容,但由于某种原因,我最终得到一个白色矩形的大小和形状WebView,没有内容,没有透明度.

如果我将WebView更改为TextView,它的内容被替换为正常,因此只有WebView控件才会出现问题:S

谁能告诉我为什么/建议修复?

最佳答案
事实证明,当使用LayoutInflater替换ViewGroup的内容时,WebView不会被清除.其他控件似乎都被删除了(或者至少findViewWithTag()为每个其他控件返回正确的引用).我刚刚在LayoutInflater之前添加了行cvFrame.removeAllViews()并解决了问题.
如果有人对此有任何更好的解释,我会以他们的方式抛出分数,否则他们将进入以太……
点击查看更多相关文章

转载注明原文:android – WebView在第二次初始化后显示为纯白框 - 乐贴网