3

我正在尝试在线性布局中设置 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);

    }
}
4

5 回答 5

5

您需要将方向设置为LinearLayoutto vertical,默认为horizontal

于 2012-10-25T06:38:52.840 回答
2

将布局更改为

 <?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. 指定方向的类型将解决您的问题

于 2012-10-25T06:39:48.587 回答
2
<?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();
            }
        });
    }
}
于 2012-10-25T06:40:38.017 回答
1

更改 LinearLayout 方向 android:orientation="vertical"并更改进度条的宽度android:layout_width="wrap_content"

还要更改 Progressbar Gravity 。android:layout_gravity="center"

在此处输入图像描述

于 2012-10-25T06:40:40.357 回答
1

你最好在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" />

于 2012-10-25T06:42:03.897 回答