简体   繁体   English

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

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

I want to add a webview to my layout.But that layout have few other widgets also.But when i run the application webview takes the entire screen and other widgets get disappear.here is my layout page.xml.... 我想在布局中添加一个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>

and in the activity class 在活动课中

    setContentView(R.layout.page);

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

It does not display the view and textView but webview get the entire screen.Please show me what i am doing wrong here.Or what is the correct way i should do. 它不显示视图和textView,但Webview可以显示整个屏幕。请在这里告诉我我做错了什么,或者我应该做的正确方法是什么。 thank you. 谢谢。

Try to set the WebViewClient , it will solve your problem. 尝试设置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);
        }
    }

change your xml like this, then your textview vill be shown on the botton, the view will be shown above the textview, the webview will be shown in the rest spase. 像这样更改您的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