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

来源:互联网 发布:吉威时代待遇知乎 编辑:程序博客网 时间:2024/06/05 13:27

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

@Overridepublic 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的内容显示是有影响的,改天再查明是什么原因。


0 0
原创粉丝点击