I just added a button to Hello WebView and everything looks right. Then I converted main.xml
from LinearLayout
to RelativeLayout
, and everything still looks right. I think you are making it too complicated. :-) Try to remove most of this initialization code from onCreate()
and just specify it in XML.
Here is my project.
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.hellowebview" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".HelloWebViewActivity" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
HelloWebViewActivity.java:
package com.example.android.hellowebview; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.webkit.WebView; import android.webkit.WebViewClient; public class HelloWebViewActivity extends Activity { WebView mWebView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_rl); mWebView = (WebView) findViewById(R.id.webview); mWebView.setWebViewClient(new HelloWebViewClient()); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.loadUrl("http://www.google.com"); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) { mWebView.goBack(); return true; } return super.onKeyDown(keyCode, event); } private class HelloWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } } }
main_ll.xml (LinearLayout):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" /> <Button android:id="@+id/mainbtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:text="@string/btn_label" /> </LinearLayout>
main_rl.xml (RelativeLayout):
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="@+id/mainbtn" /> <Button android:id="@+id/mainbtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="@string/btn_label" /> </RelativeLayout>
strings.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, HelloWebViewActivity!</string> <string name="app_name">HelloWebView</string> <string name="btn_label">Push Me</string> </resources>
Sparky
source share