android-动态添加包含WebView的Fragment显示空白

点击按钮新增包含WebView的Fragment时,WebView显示空白,其中Fragment除了WebView外还有其他控件,当使用activity打开该Fragment,LogCat输出发现已经执行了:

@Override
public void onPageFinished(WebView view, String url)

但是WebView是空白的,点击Fragment的其中一个按钮后WebView的内容就显示了,此时觉得是该Fragment没有"获得焦点",修改新增Fragment方法:

getFragmentManager().beginTransaction().add(android.R.id.content,one,"qe").commit();
原来我使用的不是android.R.id.content这个值,而是设置到自己定义的一个:

      <LinearLayout
          android:id="@+id/my_content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:focusable="true"
         />

修改为:android.R.id.content就可以显示WebView内容。android.R.id.content是根视图,使得该Fragment获得“焦点”。

后来网上查了一下别人不是用LinearLayout,修改为:

<FrameLayout
        android:id="@+id/my_fragment"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
/>
当新增一个有WebView的Fragment时,WebView显示出内容。

解决办法暂时有两种:

1、如下,使用android.R.id.content,代替你的布局,我的是LinearLayout,但是使用android.R.id.content,Fragment会覆盖原布局的内容,此内容不会清除。

getFragmentManager().beginTransaction().add(android.R.id.content,one,"qe").commit();

2、如下,使用FrameLayout代替LinearLayout,此时不会像android.R.id.content覆盖掉原布局的东西,而是根据FrameLayout所在layout进行布局

<FrameLayout
        android:id="@+id/my_fragment"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
/>

暂时只是记录解决的方法,从以上两种解决方案看出,布局对于嵌入到Fragment的WebView的内容显示是有影响的,改天再查明是什么原因。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值