如何从webview中删除额外的空格?

时间:2013-12-05 09:13:19

标签: android html webview android-webview

您好我正在使用此代码为我的webview加载一些HTML数据。它工作得非常完美,但在右边显示了额外的空白区域。

            WebView tvone = (WebView) findViewById(R.id.web);
            tvone.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
            tvone.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            pd = ProgressDialog.show(viewdetails.this, "", "Laden", true);
            pd.setCancelable(true);
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            pd.dismiss();
        }
    });

    tvone.loadDataWithBaseURL(null, html_data, "text/html", "utf-8", null);

我使用了一些使用onTouchListener的技巧,这解决了问题,但链接不起作用。我需要在webview中使用工作链接删除空间。 请帮助我是新手。

修改

enter image description here enter image description here

          <?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="match_parent"
android:background="#ffffff"
android:gravity="center" >

<WebView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_marginTop="10dp"
            />

3 个答案:

答案 0 :(得分:2)

为了您的顾虑,请尝试将WebView的宽度设置为“fill_parent”

对于Ex:

<WebView  
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

你的java类

public class AppWebView extends Activity{

    WebView webView;
    ProgressBar pBar;

    @SuppressLint("SetJavaScriptEnabled")
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.web_view);
        pBar = (ProgressBar)findViewById(R.id.progressBar1);
        //pBar.setVisibility(View.GONE);


        String newUrl;
        if (savedInstanceState == null) {
            Bundle extras = getIntent().getExtras();
            if (extras == null) {
                newUrl = null;
            } else {
                newUrl = extras.getString("url");
            }
        } else {
            newUrl = (String) savedInstanceState
                    .getSerializable("myJsonStringS");
        }

        Log.d("jitendra", newUrl);

        //SharedPreferences sp = getSharedPreferences("booking_detail", 0);
        //String jsonString = sp.getString("jsonString", "");

        webView = (WebView)findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new myWebClient());
        webView.loadUrl(newUrl);

    }
    public void moveToThanksPage()
    {
        Intent intent = new Intent(this,ThankYou.class);
        startActivity(intent);
    }
     public class myWebClient extends WebViewClient
        {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // TODO Auto-generated method stub
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                Log.d("sagarWeb", url);
                if (url.startsWith("mailto:")) {
                    String[] blah_email = url.split(":");
                    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                    emailIntent.setType("text/plain");
                    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{blah_email[1]});
                 //   emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "what_ever_you_want_the_subject_to)");
                    Log.d("NOTICE", "Sending Email to: " + blah_email[1] + " with subject: " + "what_ever_you_want_the_subject_to_be");
                    startActivity(emailIntent);
                }
                else if (url.startsWith("tel:")) {
                    Log.d("Web", "tell");
                    String uri = url;
                    Intent intent = new Intent(Intent.ACTION_CALL);
                    intent.setData(Uri.parse(uri));
                    startActivity(intent);
                }
                else if (url.endsWith("error.jsp")) {
                    Log.d("Web", "Error");
                }
                /*else if (url.contains("thankyou/app")) {

     //===================== USE UNDERMENTIONED COMMENT ON FOR SELF THANKS PAGE ==================//

                    //moveToThanksPage(); 
                }*/
                else
                {
                    view.loadUrl(url);
                    pBar.setVisibility(View.VISIBLE);
                }
                return true;

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                pBar.setVisibility(View.GONE);
                super.onPageFinished(view, url);

                //progressBar.setVisibility(View.GONE);
            }

        }
}

答案 1 :(得分:2)

这对你有帮助。您可以尝试以下代码:

 webview2.xml layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_marginBottom="55dp"
    android:orientation="vertical" >

    <WebView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webView_info"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
         >
    </WebView>
</LinearLayout>

这是你的代码。

public class WebView2 extends Activity {

String new_url;
WebSettings webSettings;
WebView webView;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview2);
    webView = (WebView) findViewById(R.id.webView_info);

    Bundle b = getIntent().getExtras();
    String information = b.getString("information");


    webView.setVerticalFadingEdgeEnabled(false);
    webSettings = webView.getSettings();
    //swebSettings.setDefaultZoom(ZoomDensity.FAR);
    webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
    webView.getSettings().setJavaScriptEnabled(true);

    webView.setWebViewClient(new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            Log.v("here","here");
            view.scrollBy(0, view.getContentHeight());
        }
    });     

    System.out.println("information: "+information);
    webView.loadDataWithBaseURL(null, information, "text/html", "UTF-8", null);

    webView.setWebViewClient(new VideoWebViewClient());
     webView.getSettings().setBuiltInZoomControls(true);
     webView.getSettings().setUseWideViewPort(false);
    webView.getSettings().setLoadWithOverviewMode(true);

}

@Override
public void onPause() {
    super.onPause();

}

@Override
public void onStop() {
    super.onStop();

}


 private class VideoWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            try{
                System.out.println("url called:::" + url);
                if (url.startsWith("tel:")) {
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
                    startActivity(intent);
                }  else if (url.startsWith("http:")
                        || url.startsWith("https:")) {

                     Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
                     startActivity(intent);

                }  else if (url.startsWith("mailto:")) {

                    MailTo mt=MailTo.parse(url);

                    //String row[] = url.split("mailto:");
                    //System.out.println("mail to: "+row[1].toString());
                    //String email_add = row[1];
                    send_email(mt.getTo());

                }
                else {
                    return false;
                }
            }catch(Exception e){
                e.printStackTrace();
            }

            return true;
        }

    }
// sending email
    public void send_email(String email_add) {
        System.out.println("Email address::::" + email_add);

        final Intent emailIntent = new Intent(
                android.content.Intent.ACTION_SEND);
        emailIntent.setType("plain/text");
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
                new String[] { email_add });
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "");
    WebView2.this.startActivity(Intent.createChooser(emailIntent,
                "Send mail..."));

    }

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();

}

}

答案 2 :(得分:0)

内容超出了宽度。所以这是在内容中添加的。

           .div{white-space: -moz-pre-wrap !important;  /* Mozilla, since 1999 */    
                white-space: -pre-wrap;      /* Opera 4-6 */     white-space: -o-pre-wrap;                                                                                                         
                white-space: pre-wrap;       /* css-3 */     word-wrap: break-word;       /* Internet Explorer 5.5+ */    
                word-break: break-all;     
                white-space: normal;}