簡體   English   中英

如何在LinearLayout中設置WebView

[英]How to set WebView inside a LinearLayout

我試圖在linearlayout中設置webview。 我剛剛在線性布局中創建了一個xml線性布局文件,進度條和webview。

<?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" >

    <ProgressBar
        android:id="@+id/progWeb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:max="100"
        android:visibility="invisible" />

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

</LinearLayout>

和java文件只顯示進度條。 此代碼未顯示Webview

public class MainActivity extends Activity {

    private WebView web;
    private ProgressBar progBar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.activity_main);
        web = (WebView) findViewById(R.id.web);
        progBar = (ProgressBar) findViewById(R.id.progWeb);

        progBar.setVisibility(ProgressBar.INVISIBLE);
        String url = "http://www.google.com.pk/";

        web.getSettings().setJavaScriptEnabled(true);
        web.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                System.out.println(progress);
                progBar.setProgress(progress);

                if (progress == 100) {
                    progBar.setVisibility(ProgressBar.INVISIBLE);
                    progBar.setProgress(0);
                } else {
                    progBar.setVisibility(ProgressBar.VISIBLE);
                }
            }
        });

        web.loadUrl(url);

    }
}

您需要將LinearLayout的方向設置為vertical ,默認為horizontal

將您的布​​局更改為

 <?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:orientation="vertical"//==============> Here
android:layout_height="fill_parent" >

<ProgressBar
    android:id="@+id/progWeb"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/darker_gray"
    android:max="100"
    android:visibility="invisible" />

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

</LinearLayout>

默認情況下,LinearLayout將設置為Horizontal方向。 指定方向類型將解決您的問題

<?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" >

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

</LinearLayout>

你的活動

public class YourActivityName extends Activity{

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.aboutus);

        String url = "http://www.google.com.pk/";
        getWindow().setFeatureInt(
            Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON); 
        webView=(WebView)findViewById(R.id.webViewLoad); 
        webView.getSettings().setJavaScriptEnabled(true);   
        webView.getSettings().setSupportZoom(true);         
        webView.getSettings().setBuiltInZoomControls(true);
        webView.loadUrl(url);

        webView.setWebViewClient(new WebViewClient() {
            ProgressDialog progressDialog = new ProgressDialog(YourActivity.this);

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                Log.e("I am  loading Here ","Start");
                progressDialog.setTitle("Loading");
                progressDialog.setMessage("Please wait....");
                progressDialog.show();
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.e("I am  loading Here ","Override");
                view.loadUrl(url);
                return true;    
            }

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

更改LinearLayout Orientation android:orientation="vertical"並更改Progress Bar的寬度android:layout_width="wrap_content"

同時更改Progressbar Gravity。 android:layout_gravity="center"

在此輸入圖像描述

你最好在RelativeLayout里面使用Webview而不是Linear,它應該解決問題。

<ProgressBar
    android:id="@+id/progWeb"
    style="@style/progressbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/darker_gray"
    android:max="100"
    android:visibility="invisible" />

<WebView
    android:id="@+id/web"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/progWeb"
    android:layout_marginTop="28dp" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM