如何在Webview Android中对齐进度条?

ar7v8xwq  于 2023-02-10  发布在  Android
关注(0)|答案(3)|浏览(93)

我写了简单的网络视图应用程序在Android中,但进度条显示的是应用程序的顶部点击here查看图像。实际上,我想这样做点击here
这是我的主要活动

public class MainActivity extends AppCompatActivity implements MyWebChromeClient.ProgressListener {

WebView wv;
private ProgressBar mProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    wv=(WebView) findViewById(R.id.webview);
    WebSettings settings = wv.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.getLoadsImagesAutomatically();
    settings.getBuiltInZoomControls();
    settings.setSaveFormData(true);
    settings.setEnableSmoothTransition(true);
    settings.setNeedInitialFocus(true);
    settings.setSavePassword(true);
    settings.setDomStorageEnabled(true);
    settings.setPluginState(WebSettings.PluginState.ON);
    // add progress bar
    mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
    wv.loadUrl("https://www.w3schools.com/");
    wv.setWebChromeClient(new MyWebChromeClient(this));
    wv.setWebViewClient(new WebViewClient(){
        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);
            mProgressBar.setVisibility(View.VISIBLE);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            mProgressBar.setVisibility(View.GONE);

        }
    });
}

@Override
public void onBackPressed() {
    if(wv.canGoBack()){

        wv.goBack();
    }
    else{
        super.onBackPressed();
    }
}

@Override
public void onUpdateProgress(int progressValue) {
    mProgressBar.setProgress(progressValue);
    if (progressValue == 100) {
        mProgressBar.setVisibility(View.INVISIBLE);
    }
}}

在这个布局中我添加了webview和进度条。

    • 活动_主要. xml**
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.johnson.apple.MainActivity">
    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
    <ProgressBar
        android:id="@+id/progressBar"
        android:minHeight="2dip"
        android:maxHeight="2dip"
        android:progressDrawable="@drawable/bg_progress_bar_webview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="@android:style/Widget.ProgressBar.Horizontal" />
</RelativeLayout>

希望任何人都能帮助我Thnaks提前

aydmsdu9

aydmsdu91#

添加到进度栏中

android:layout_gravity="center_vertical|center_horizontal"
android:layout_centerInParent="true"

所以进度条应该是

<ProgressBar
android:id="@+id/progressBar"
android:minHeight="2dip"
android:maxHeight="2dip"
android:progressDrawable="@drawable/bg_progress_bar_webview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_centerInParent="true"
style="@android:style/Widget.ProgressBar.Horizontal" />
bnl4lu3b

bnl4lu3b2#

android:layout_centerInParent="true"添加到进度栏布局
像这样:

<ProgressBar
android:id="@+id/progressBar"
android:minHeight="2dip"
android:maxHeight="2dip"
android:progressDrawable="@drawable/bg_progress_bar_webview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
style="@android:style/Widget.ProgressBar.Horizontal" />
qaxu7uf2

qaxu7uf23#

我对此做了一些改进,答案是修改activity_xml

<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.app.superdeveloper.weblc.learncoding.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />
    <ProgressBar
        android:id="@+id/progressBar"
        android:minHeight="2dip"
        android:maxHeight="2dip"
        android:progressDrawable="@drawable/bg_progress_bar_webview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="@android:style/Widget.ProgressBar.Horizontal" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main"/>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@android:drawable/ic_dialog_email" /></android.support.design.widget.CoordinatorLayout>

现在起作用了

相关问题