標籤:des android style blog http ar io color sp
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include layout="@layout/uzone_top_bar" /> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center_vertical" > <TextView android:id="@+id/uzone_top_TextView_title" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerVertical="true" android:gravity="center" android:text="" android:textSize="18sp" /> <RelativeLayout android:id="@+id/uzone_top_RelativeLayout_cancel" android:layout_width="50dp" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:gravity="center" > <ImageView android:id="@+id/uzone_top_ImageView_cancel" android:layout_width="20dp" android:layout_height="20dp" android:layout_marginLeft="10dp" android:paddingLeft="10dp" android:paddingTop="10dp" /> </RelativeLayout> <ImageView android:id="@+id/uzone_top_ImageView_line" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginRight="2dp" android:layout_toLeftOf="@+id/uzone_top_Button_ok" android:visibility="gone" /> <Button android:id="@+id/uzone_top_Button_ok" android:layout_width="70dip" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_gravity="center" android:text="按鈕" android:textColor="@android:color/white" android:textSize="18sp" android:visibility="gone" /></RelativeLayout>
public class WebBrowserActivity extends Activity{ /** * UshequMobile地址 */ public static final String URL_PREFIX = "http://10.10.9.51:8080/UshequMobile/"; public static final String USERAGENT = "haiersoft.webbrowser"; private WebView webView; private TextView title; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.web_browser); //標題 title = (TextView) findViewById(R.id.uzone_top_TextView_title); //返回按鈕 RelativeLayout back = (RelativeLayout) findViewById(R.id.uzone_top_RelativeLayout_cancel); back.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); webView = (WebView) findViewById(R.id.webview); configWebView(); Intent intent = getIntent(); String url = intent.getStringExtra("url"); if(null != url) { webView.loadUrl(url); } else { Toast.makeText(this, "url為空白", Toast.LENGTH_SHORT).show(); } } private void configWebView() { WebSettings webSettings = webView.getSettings(); webSettings.setSupportZoom(false);//設定不可縮放 webSettings.setJavaScriptEnabled(true); //設定支援javascript webSettings.setUserAgentString(USERAGENT);//設定值用於Web服務判斷訪問來源
//WebViewClient就是協助WebView處理各種通知、請求事件的,具體來說包括: webView.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); } public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { super.onReceivedError(view, errorCode, description, failingUrl); Toast.makeText(getApplicationContext(), description, Toast.LENGTH_LONG).show(); } });
//WebChromeClient主要處理解析,渲染網頁等瀏覽器做的事情
//WebChromeClient是輔助WebView處理Javascript的對話方塊,網站表徵圖,網站title,載入進度等
webView.setWebChromeClient(new WebChromeClient() //遊覽器 { @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) //對話方塊 {// result.confirm();// return true; return super.onJsAlert(view, url, message, result); } @Override public void onReceivedTitle(WebView view, String title) //標題 { super.onReceivedTitle(view, title); WebBrowserActivity.this.title.setText(title); } }); } @Override protected void onDestroy() { super.onDestroy(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) //按鍵響應 { if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) { webView.goBack(); return true; } return super.onKeyDown(keyCode, event); } @Override protected void onPause() { super.onPause(); } @Override protected void onRestart() { super.onRestart(); } @Override protected void onResume() { super.onResume(); } }
android WebView 顯示網頁