Android 进度条及;在我的代码中,加载文本并没有显示所有内容看起来都是正确的

Android 进度条及;在我的代码中,加载文本并没有显示所有内容看起来都是正确的,android,android-webview,android-progressbar,Android,Android Webview,Android Progressbar,我使用的是包含链接的本地html文件,但当我点击链接时,它不会显示进度条&在webview中加载文本 这是我的Mainactivity.java文件 MainActivity.java 公共类MainActivity扩展了AppCompatActivity 实现NavigationView.OnNavigationItemSelectedListener{ WebView wv; @Override protected void onCreate(Bundle savedInstanceSta

我使用的是包含链接的本地html文件,但当我点击链接时,它不会显示进度条&在webview中加载文本 这是我的Mainactivity.java文件

MainActivity.java

公共类MainActivity扩展了AppCompatActivity 实现NavigationView.OnNavigationItemSelectedListener{

WebView wv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    wv = (WebView)findViewById(R.id.webview);
    wv.loadUrl("file:///android_asset/www/index.html");
    wv.setWebViewClient(new MyWebViewClient());
    WebSettings webSettings = wv.getSettings();
    webSettings.setJavaScriptEnabled(true);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_exit) {
        System.exit(0);
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

private class MyWebViewClient extends WebViewClient {
    TextView txt =  (TextView) findViewById(R.id.txtload);
    ProgressBar pbar = (ProgressBar) findViewById(R.id.pg1);

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
        wv.loadUrl("file:///android_asset/www/error.html");
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        System.out.println("loading... please wait");

        pbar.setVisibility(View.VISIBLE);
        txt.setVisibility(View.VISIBLE);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        System.out.println("finished loading");

        pbar.setVisibility(View.GONE);
        txt.setVisibility(View.GONE);
    }
}
}
content.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main">

    <TextView
        android:id="@+id/txtload"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Loading... Please Wait"
        android:textColor="@android:color/holo_orange_dark"
        android:textSize="14sp"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="209dp" />

    <ProgressBar
        android:id="@+id/pg1"
        style="@android:style/Animation.Dialog"
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:gravity="center"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="4dp" />

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/background_light"></WebView>

</android.support.constraint.ConstraintLayout>

Android studio按照视图在XML文件中的显示顺序显示视图。您需要做的是对视图进行重新排序。Web视图应先显示,然后是文本视图,然后是进度视图。 问题是,它在应用程序中,并且正在显示,只是在运行应用程序时不向用户或您显示。网络视图正在模糊其他视图。查看它是否有帮助,并标记为正确答案