有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我想在布局中添加一个webview。但这种布局也很少有其他小部件。但是当我运行应用程序时,webview占据了整个屏幕,其他小部件也消失了。这是我的布局页面。xml

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

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

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

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


</LinearLayout>

在活动课上

    setContentView(R.layout.page);

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

它不显示视图和文本视图,但webview会显示整个屏幕。请告诉我我做错了什么。或者我应该怎么做。 多谢各位


共 (2) 个答案

  1. # 1 楼答案

    像这样更改您的xml,然后您的textview将显示在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>
    
  2. # 2 楼答案

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