Android Add Button on WebView?
How can I add a button on a WebView
?. I have a WebView and I want to show a popup. For that I nee开发者_JAVA百科d to add a button on the bottom-left corner of WebVew. How can I do this?
I would use a RelativeLayout. I like using it a lot. Its a great way to easily place and organize views,buttons,layouts,etc...
Some Example code:
<?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"
android:background="#FFFFD0">
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="@+id/My_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:text="My Button!" />
</RelativeLayout>
I think the views and buttons will be drawn in order from top to bottom in the XML, but it may be the other way around.use margins like :
android:layout_marginLeft="15dip"
and android:layout_marginBottom="10dip"
to help adjust the position.
What kind of content is in the WebView
? Is it some HTML that you can control / modify?
- If yes, just add a
<button>
tag and position it accordingly using CSS. - If no, use Wayner's solution.
package com.webview;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;
public class webview extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_PROGRESS);
WebView webview = new WebView(this);
Button btnTag = new Button(this);
btnTag.setText("Button");
btnTag.setId(1);
webview.addView(btnTag);
setContentView(webview);
webview.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setProgress(progress * 1000);
}
});
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description,
Toast.LENGTH_SHORT).show();
}
});
webview.loadUrl("http://www.google.com/");
}
}
精彩评论