我使用包含链接的本地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>
答案 0 :(得分:0)
Android studio按照它们在XML文件中出现的顺序显示视图。您需要做的是重新排序视图。首先应该是webview,然后是Textview,然后是进度视图。 事情是,它在应用程序中显示,而不是在运行应用程序时向用户或您显示。 webview模糊了其他人。看看它是否有用并标记为正确的答案。