问题
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....
<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. thank you.
回答1:
Try to set the WebViewClient, it will solve your problem.
/** 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:
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.
<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>
来源:https://stackoverflow.com/questions/8005230/how-add-a-webview-to-a-layout-that-have-more-widgets