gpt4 book ai didi

java - android 在同一布局上设置的 webview 之间切换

转载 作者:行者123 更新时间:2023-12-02 07:43:56 25 4
gpt4 key购买 nike

我试图在两个不同的 Activity 之间切换,每个 Activity 都包含一个 WebView 。切换它们时我遇到了很多内存问题,我现在正在尝试以下操作:我在同一布局上设置了 VISIBLE 或 GONE 两个 webview。当我从 webviewA 按下按钮时,在 webviewB 上加载 url,当 webviewB onPageFinished() 时,A 设置为 GONE,B 设置为 VISIBLE。从 B 到 A 相同。问题是 url 仅在第一次加载...

两个 webview 设置在相同的布局上,例如,

<RelativeLayout
android:id="@+id/aLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/barraSocial">

<WebView
android:id="@+id/webviewA"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:autoLink="web"
android:textColor="@android:color/black"
android:scrollbars="none"/>
</RelativeLayout>

<RelativeLayout
android:id="@+id/bLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/barraSocial">
<WebView
android:id="@+id/webviewB"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:autoLink="web"
android:textColor="@android:color/black"
android:scrollbars="none"/>
</RelativeLayout>

onCreate Activity ,

final RelativeLayout LayoutA = (RelativeLayout)findViewById(R.id.aLayout);
final RelativeLayout LayoutB = (RelativeLayout)findViewById(R.id.bLayout);

webviewA = (WebView) findViewById(R.id.webviewA);
webviewB = (WebView) findViewById(R.id.webviewB);

在webview上单击按钮,

webviewB.loadUrl(UrlVista);

然后,来自 webviewB 的 onPageFinished() 被触发,

 webviewB.setWebViewClient(new WebViewClient() 
{
@Override
public void onPageFinished(WebView view, String url)
{
LayoutA.setVisibility(View.GONE);
LayoutB.setVisibility(View.VISIBLE);

最佳答案

您有两个相对布局。一个人本质上是将另一个人推离屏幕。与您拥有的不同,只需使用 2 个 Web View 的一个相对布局即可:

在您的 xml 文件中:

<RelativeLayout android:id="@+id/aLayout"                 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/barraSocial">

<WebView android:id="@+id/webviewA"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:autoLink="web"
android:textColor="@android:color/black"
android:scrollbars="none"
android:alignParentLeft="true"
android:alignParentTop="true"//>

<WebView android:id="@+id/webviewB"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:autoLink="web"
android:textColor="@android:color/black"
android:scrollbars="none"
android:alignParentLeft="true"
android:alignParentTop="true"/>

</RelativeLayout>

然后在你的 onPageFinished 中

webviewB.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url{
webviewA.setVisibility(View.GONE);
webviewB.setVisibility(View.VISIBLE);
}
});

这本质上是让您设置 WebView 本身的可见性,而不是布局。在这种情况下,布局可以是一个在另一个之上,并且您可以使用它们的可见性来基本上关闭和打开一个布局。

关于java - android 在同一布局上设置的 webview 之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11161708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com