![](/img/trans.png)
[英]Google IMA webview is not showing when used inside react native on android
[英]Android WebView: ProgressBar not showing (Used Google's tutorial)
在WebView中加載網頁時,進度條不顯示。 我的XML代碼段:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" >
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/webView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
我的MainActivity.java代碼段:
public class Main1Activity extends ActionBarActivity
{
private WebView webView;
public void onCreate( Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_PROGRESS);
super.onCreate(savedInstanceState);
webView = new WebView(this);
setContentView(webView);
webView.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 100);
}
});
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
webView.loadUrl("http://developer.android.com/");
}
// Go back to previous upon clicking back button
// instead of closing app.
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack())
{
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
return super.onOptionsItemSelected(item);
}
}
我已經檢查了關於Stackoverflow的所有資源並發布了此資源,因為我沒有找到任何解決方案。 起初,這本來應該很容易,但是這花了5天多的時間。
我認為您正在嘗試混合使用2種不同的ProgressBar
實現,第一種使用getWindow().requestFeature(Window.FEATURE_PROGRESS);
第二個使用定義到主布局中的ProgressBar
。 我認為最好的選擇是在布局內使用ProgressBar。
所以,你需要改變一點點你的代碼,使用onPageStarted()
來顯示ProgressBar
和onPageFinished()
來隱藏它:
public class MainActivity extends ActionBarActivity
{
private WebView webView;
private ProgressBar progressBar;
public void onCreate( Bundle savedInstanceState) {
//getWindow().requestFeature(Window.FEATURE_PROGRESS); // You don´t need this!
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar)findViewById(R.id.progressBar);
/*webView = new WebView(this);
setContentView(webView);*/ //Using this your WebView was covering all your screen :P!
webView = (WebView)findViewById(R.id.webView);//Use the WebView defined in your layout.
webView.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 100);
}
});
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressBar.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.INVISIBLE);
super.onPageFinished(view, url);
}
});
webView.loadUrl("http://developer.android.com/");
}
// Go back to previous upon clicking back button
// instead of closing app.
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack())
{
webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
return super.onOptionsItemSelected(item);
}
}
並且您的布局也發生了一點變化:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:visibility="visible" />
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView"
android:layout_below="@+id/progressBar"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
5天還在計數? 沒門 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.