【问题标题】:How add a webview to a layout that have more widgets如何将 webview 添加到具有更多小部件的布局中
【发布时间】:2011-11-04 05:27:06
【问题描述】:

我想在我的布局中添加一个 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 获得整个屏幕。请告诉我我在这里做错了什么。或者我应该做的正确方法是什么。 谢谢。

【问题讨论】:

  • 这里你可以得到答案stackoverflow.com/questions/3382188/…
  • 感谢 Hossain,我认为重定向会引起 CommonsWare 那里所说的问题。 google.com 重定向到 google.lk,然后浏览器让我抓狂。

标签: java android android-layout android-widget


【解决方案1】:

尝试设置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);
        }
    }

【讨论】:

    【解决方案2】:

    像这样更改你的xml,然后你的textview将显示在botton上,视图将显示在textview上方,webview将显示在rest spase中。

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-26
      • 2011-03-11
      • 1970-01-01
      • 1970-01-01
      • 2013-05-01
      • 2018-08-14
      • 2012-06-03
      • 1970-01-01
      相关资源
      最近更新 更多