当前位置: 首页 > 工具软件 > TitleBar > 使用案例 >

android webview titlebar,自定义titlebar和webview的使用

楚宏胜
2023-12-01

需要添加titlebar的布局,修改titlebar的高度,以及详细指明了popupwindow的使用

package com.estrongs.android.ui.homepage.blocks;

import com.estrongs.android.pop.R;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.MenuItem;

import android.view.MotionEvent;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.View.OnTouchListener;

import android.view.ViewGroup.LayoutParams;

import android.view.Window;

import android.webkit.WebChromeClient;

import android.webkit.WebView;

import android.webkit.WebViewClient;

import android.widget.ImageButton;

import android.widget.PopupWindow;

import android.widget.TextView;

public class CustomBrowser extends Activity {

WebView webView;

ImageButton back;

TextView articlTitle;

ImageButton customBrowserMenu;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

//getWindow().requestFeature(Window.FEATURE_PROGRESS);

setContentView(R.layout.custom_browser);

getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_browser_titlebar);

webView = (WebView) findViewById(R.id.custom_webview);

webView.getSettings().setJavaScriptEnabled(true);

webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

webView.getSettings().setSupportMultipleWindows(true);

// webView.setWebViewClient(new WebViewClient());

// webView.setWebChromeClient(new WebChromeClient());

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.setWebChromeClient(new WebChromeClient() {

public void onProgressChanged(WebView view, int progress) {

// Activities and WebViews measure progress with different

// scales.

// The progress meter will automatically disappear when we reach

// 100%

CustomBrowser.this.setProgress(progress * 100);

}

});

webView.loadUrl("http://baidu.com");

back = (ImageButton) findViewById(R.id.custom_browser_back);

articlTitle = (TextView) findViewById(R.id.custome_browser_article_title);

customBrowserMenu = (ImageButton) findViewById(R.id.custom_browser_menu);

customBrowserMenu.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View view) {

// TODO Auto-generated method stub

showPopupWindow(view);

}

});

}

private void showPopupWindow(View view) {

View contentView = LayoutInflater.from(this).inflate(R.layout.custom_browser_popupwindow, null);

final PopupWindow popupWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT,

LayoutParams.WRAP_CONTENT, true);

popupWindow.setTouchInterceptor(new OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

Log.i("mengdd", "onTouch : ");

return false;

// 这里如果返回true的话,touch事件将被拦截

// 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss

}

});

// 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框

// 我觉得这里是API的一个bug

popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.background));

// 设置好参数之后再show

popupWindow.showAsDropDown(view);

// popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

menu.add(Menu.NONE, 0, 0, "后退");

menu.add(Menu.NONE, 1, 1, "添加为收藏");

menu.add(Menu.NONE, 2, 2, "分享");

menu.add(Menu.NONE, 3, 3, "在浏览器中打开");

menu.add(Menu.NONE, 4, 4, "添加到桌面");

menu.add(Menu.NONE, 5, 5, "窗口");

return super.onCreateOptionsMenu(menu);

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

// TODO Auto-generated method stub

if (item.getItemId() == 1) {

System.out.println("#########################");

}

switch (item.getItemId()) {

case 0:

System.out.println("**********************0 hou tui");

break;

case 1:

System.out.println("**********************1 tian jia shou cang");

break;

case 2:

System.out.println("**********************2 fen xiang");

break;

case 3:

System.out.println("**********************3 zai ");

break;

case 4:

System.out.println("**********************4 tian jia dao zuo m ");

break;

default:

break;

}

return super.onOptionsItemSelected(item);

}

public void reload(View v) {

webView.reload();

}

public void stop(View v) {

webView.stopLoading();

}

public void back(View v) {

webView.goBack();

}

public void forward(View v) {

webView.goForward();

}

}

 类似资料: