簡體   English   中英

如何將WebView添加到具有更多小部件的布局中

[英]How add a webview to a layout that have more widgets

我想在布局中添加一個webview。但是該布局中也沒有其他小部件。但是當我運行應用程序時,webview占據了整個屏幕,其他小部件消失了。這是我的布局page.xml ....

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:orientation="vertical" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" >

<WebView android:layout_width="match_parent" android:layout_height="200dp" android:id="@+id/web"></WebView>

<View   android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:layout_height="2px" android:background="#ffffff" android:layout_width="fill_parent" ></View>

<TextView android:text="Simple text view"  android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>


</LinearLayout>

在活動課中

    setContentView(R.layout.page);

    WebView wv1=(WebView)findViewById(R.id.web);
    wv1.loadUrl("http://google.com");

它不顯示視圖和textView,但Webview可以顯示整個屏幕。請在這里告訴我我做錯了什么,或者我應該做的正確方法是什么。 謝謝。

嘗試設置WebViewClient ,它將解決您的問題。

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

         WebView wv1=(WebView)findViewById(R.id.web1);
         wv1.setWebViewClient(new myClient());
         wv1.loadUrl("http://google.com");
    }

    class myClient extends WebViewClient
    {
        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub

            view.loadUrl(url);
            return true;
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
            // TODO Auto-generated method stub
            super.onReceivedError(view, errorCode, description, failingUrl);
        }
    }

像這樣更改您的xml,然后您的textview vill將顯示在botton上,該視圖將顯示在textview的上方,webview將顯示在其余的空間中。

<RelativeLayout
  ...
>
<TextView
    android:layout_alignParentBottom="true"
    android:id="@+id/sample_text"
    ...
/>
<View
    android:layout_above="@id/sample_text"
    android:id="@+id/sample_view"
    ...
>
<WebView
    android:layout_above="@id/sample_view"
    ...
/>
</RelativeLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM