WebView PopWindow Demo
package com.example.testpop; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.Menu; import android.view.MenuItem; import android.view.MenuItem.OnMenuItemClickListener; import android.view.View; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import android.widget.LinearLayout.LayoutParams; import android.widget.PopupWindow; import android.widget.Toast; //主页面放一个WebView //ActionBar 上面一个menu,点击menu弹出PopWindow //PopWindow上面放GridView布局,ItemClick监听器, //判断点击的item ID执行不同方法 reload(),goback(),goforward() public class MainActivity extends Activity implements OnItemClickListener, OnMenuItemClickListener { private PopupWindow mPopupWindow; private View mViewiewMain; private WebView mWebView; private MenuItem mMenuItem; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mViewiewMain = getLayoutInflater() .inflate(R.layout.activity_main, null); // button = (Button) findViewById(R.id.button1); mWebView = (WebView) findViewById(R.id.webView1); mWebView.loadUrl("http://3gqq.qq.com/"); // WebView重定向 mWebView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); mPopupWindow = new PopupWindow(this); // DisplayMetrics outMetrics = new DisplayMetrics(); // getWindowManager().getDefaultDisplay().getMetrics(outMetrics); // popupWindow.setWidth(outMetrics.widthPixels / 5 * 3); // popupWindow.setHeight(outMetrics.heightPixels / 5 * 3); // 代码设置PopWindow的宽和高 mPopupWindow.setWidth(LayoutParams.MATCH_PARENT); mPopupWindow.setHeight(LayoutParams.WRAP_CONTENT); // PopWindow的布局 View view = getLayoutInflater().inflate(R.layout.window, null); GridView gridView = (GridView) view.findViewById(R.id.gridView1); gridView.setAdapter(new GridViewAdapter(this)); gridView.setOnItemClickListener(this); mPopupWindow.setContentView(view); // mPopupWindow 获取焦点 mPopupWindow.setFocusable(true); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); // 找到菜单Menu并设置监听器 mMenuItem = menu.findItem(R.id.action_menu1); mMenuItem.setOnMenuItemClickListener(this); return true; } // // public void btn(View v) { // mPopupWindow.showAtLocation(mViewiewMain, Gravity.BOTTOM, 10, 0); // } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // Toast.makeText(this, position + "", Toast.LENGTH_SHORT).show(); switch (position) { case 0: // 刷新 mWebView.reload(); mPopupWindow.dismiss(); break; case 1: // 后退 if (mWebView.canGoBack()) { mWebView.goBack(); } else { Toast.makeText(this, "不能退了!!!", Toast.LENGTH_SHORT).show(); } mPopupWindow.dismiss(); break; case 2: // 前进 if (mWebView.canGoForward()) { mWebView.goForward(); } else { Toast.makeText(this, "不能进了!!!", Toast.LENGTH_SHORT).show(); } mPopupWindow.dismiss(); break; } } @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { // 显示PopWindow // showAtLocation case R.id.action_menu1: mPopupWindow.showAtLocation(mWebView, Gravity.BOTTOM, 0, 0); break; } return false; } }
引用
GridView的适配器:
package com.example.testpop; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; public class GridViewAdapter extends BaseAdapter { private Context context; private Integer[] images = { R.drawable.card_refresh, R.drawable.back, R.drawable.forward }; public GridViewAdapter(Context context) { super(); this.context = context; } @Override public int getCount() { return images.length; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { imageView = new ImageView(context); imageView.setPadding(8, 8, 8, 8); imageView.setLayoutParams(new GridView.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); } else { imageView = (ImageView) convertView; } imageView.setImageResource(images[position]); return imageView; } }
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28