Android removes the webpage title and sets it as a custom title when developing WebView.

Source: Internet
Author: User

Android removes the webpage title and sets it as a custom title when developing WebView.

In IOS, just remove the title with two lines of code similar to the following

[Js appendString: @ var title = document. getElementsByClassName ('navbar-top') [0];
[Js appendString: @ title. parentNode. removeChild (title);];

It is really difficult to remove the webpage title on android. It took me a few days to complete the process. Currently, this is definitely the first person to replace the title with a custom title. The text of the title is changed on the Internet. Sometimes the title of the H5 page to be loaded is different from the style of the app. You must remove the title bar and replace it with your own title bar. First go to the web page and finally change the title

Source code of the previous webpage

1. initialize WebView and set the following attributes.

 

Webview = (WebView) findViewById (R. id. newer_guide_webview); // sets the WebView attribute and can execute the Javascript script webview. getSettings (). setJavaScriptEnabled (true); // load the webview of the page to be displayed. setWebViewClient (new MyWebViewClient (); webview. loadUrl (mUrl); // sets the webview of the Web View. getSettings (). setJavaScriptEnabled (true); webview. setScrollBarStyle (View. SCROLLBARS_INSIDE_OVERLAY );
2. Customize WebviewClient, that is, inherit WebViewClient and implement its method, and add the jsoup jar package (this method is not good and there is a correct method later)

 

// Web View
Private class MyWebViewClient extends WebViewClient {
@ Override
Public boolean shouldOverrideUrlLoading (WebView view, String url ){
View. loadUrl (url );
Return true;
}
@ Override
Public void onPageStarted (WebView view, String url, Bitmap favicon ){
Super. onPageStarted (view, url, favicon );
If (! FirstLink) {// must be added. Otherwise, the WebView interface will be refreshed continuously.
FirstLink = true;
/** Start with the main code for removing the title **/

Document doc = null;
Try {
Doc = Jsoup. connect (mUrl). get ();
} Catch (IOException e ){
E. printStackTrace ();
}
TitleString = doc. getElementsByClass (nav-title). text ();
System. out. println (******* + titleString );

/** To show the beginning of H5, ** // ** the above Web page diagram must be understood to make a successful patchwork, <> An Element contains all its contents **/
Elements content = doc. getAllElements ();
String html = +

;
Html + = content. get (2). toString () ++; // you have to write a for loop for this number. Print each element and replace the number.
Html + = content. get (16). toString ();
/** Patchwork to show the start of H5 **/
View. loadDataWithBaseURL (mUrl, html, text/html, UTF-8, null); // declare firstLink as long as this sentence exists, or refresh the interface all the time.

 

/** Start with the main code for removing the title **/
}

}

@ Override
Public void onPageFinished (WebView view, String url ){
Super. onPageFinished (view, url );

// Actions on the title bar
TextView actionBarTitle = (TextView) findViewById (R. id. actionbar_middle );
ActionBarTitle. setText (titleString );
TextView leftTv = (TextView) findViewById (R. id. actionbar_left );
/**
* The following attributes must be set. Otherwise, the left-click header is not displayed.
*/
Drawable drawable = getResources (). getDrawable (R. drawable. btn_back );
Drawable. setBounds (0, 0, drawable. getMinimumWidth (), drawable. getMinimumHeight ());
LeftTv. setCompoundDrawables (drawable, null, null );
LeftTv. setVisibility (View. VISIBLE );

}
}

The above method will cause serious computer memory consumption. When I test it, it will lead to a debugging code, and 8 GB memory computers will all be tight, mainly because doc = Jsoup. connect (mUrl ). get. In addition, the final effect is that the title bar is removed, but the title bar is blank, that is, the layout is not removed.The IOS processing method is to move the Y coordinate of WebView to a segment, and increase the height of WebView to the height of the move up.It is useless after being set on the android end, And the WebView cannot be touched.

You can only remove the title, but you cannot remove the blank title.

In another way, set the layout as a form and hover the title bar on the top of the structure to overwrite the title bar of the webpage. Layout and the entire code

 

 <Framelayout android: layout_height = "match_parent" android: layout_width = "match_parent" xmlns: actionbar = "http://schemas.android.com/tools" xmlns: android = "http://schemas.android.com/apk/res/android">
         
 
         
             
              
           
   
  
 </Framelayout>
The entire code of the activity, without the second step of the code above

 

 

Package com. pitaya. daokoudai. view. activity. more; import android. app. activity; import android. graphics. bitmap; import android. graphics. drawable. drawable; import android. OS. bundle; import android. view. keyEvent; import android. view. view; import android. webkit. webView; import android. webkit. webViewClient; import android. widget. textView; import com. pitaya. daokoudai. r; import com. pitaya. daokoudai. common. util. commonUtil; import com. pitaya. daokoudai. common. util. viewHolderUtils;/***** website announcement */public class WebsiteNoticeActivity extends Activity {private WebView webview;/***** url to be accessed */private String mUrl; private String titleString; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_website_notice); mUrl = getIntent (). getStringExtra (url); initWidget () ;}@ Override protected void onResume () {super. onResume ();} private void initWidget () {webview = (WebView) findViewById (R. id. newer_guide_webview); // sets the WebView attribute and can execute the Javascript script webview. getSettings (). setJavaScriptEnabled (true); // load the webview of the page to be displayed. setWebViewClient (new MyWebViewClient (); webview. loadUrl (mUrl); // sets the webview of the Web View. getSettings (). setJavaScriptEnabled (true); webview. setScrollBarStyle (View. SCROLLBARS_INSIDE_OVERLAY);} @ Override // set the rollback // overwrite the onKeyDown (int keyCoder, KeyEvent event) method of the Activity class public boolean onKeyDown (int keyCode, KeyEvent event) {if (keyCode = KeyEvent. KEYCODE_BACK) & webview. canGoBack () {webview. goBack (); // goBack () indicates that return true on the previous page of WebView;} return false ;} // Web view private class MyWebViewClient extends WebViewClient {@ Override public boolean shouldOverrideUrlLoading (WebView view, String url) {view. loadUrl (url); return true ;}@ Override public void onPageStarted (WebView view, String url, Bitmap favicon) {super. onPageStarted (view, url, favicon); CommonUtil. showProgressDialog (WebsiteNoticeActivity. this, getString (R. string. doing), true) ;}@ Override public void onPageFinished (WebView view, String url) {super. onPageFinished (view, url); CommonUtil. closeProgressDialog (); TextView actionBarTitle = ViewHolderUtils. find (WebsiteNoticeActivity. this, R. id. actionbar_middle); actionBarTitle. setText (getResources (). getString (R. string. more_item_notice); TextView leftTv = ViewHolderUtils. find (WebsiteNoticeActivity. this, R. id. actionbar_left);/*** the following attributes must be set. Otherwise, the left-click header does not display */Drawable drawable = getResources (). getDrawable (R. drawable. btn_back); drawable. setBounds (0, 0, drawable. getMinimumWidth (), drawable. getMinimumHeight (); leftTv. setCompoundDrawables (drawable, null, null); leftTv. setVisibility (View. VISIBLE) ;}} public void onClick (View v) {if (v. getId () = R. id. actionbar_left) {if (webview. canGoBack () {webview. goBack () ;}else {finish ();}}}}
The coverage method is perfect, and the second step above may also be acceptable, but I cannot use it when I move Y and increase. Ah, android is not as simple as IOS. You can see the world of android. After finishing, close the job.

 

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.