我一直在努力寻找一种如何在webview中添加底栏的解决方案,但到目前为止还无法找到解决方案。欢迎任何帮助。
这是我的activity_main.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
xmlns:design="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity"
android:theme="@style/Theme.AppCompat.Light"
android:orientation="vertical">
<WebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
design:menu="@menu/menu_navigation"
/>
</android.support.design.widget.CoordinatorLayout>
这是我的mainActivity文件。我仍然是java的新手,我将在我的应用程序中添加不同的功能,包括处理呼叫共享,但我遇到的问题是它看起来不起作用的底栏
public class MainActivity extends AppCompatActivity {
WebView web;
ProgressBar progressBar;
ProgressDialog mProgressDialog;
private BottomNavigationView bottomNavigationView;
private TextView infoTextView;
private ValueCallback<Uri> mUploadMessage;
public ValueCallback<Uri[]> uploadMessage;
public static final int REQUEST_SELECT_FILE = 100;
private final static int FILECHOOSER_RESULTCODE = 1;
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == mUploadMessage) return;
Uri result = intent == null || resultCode != RESULT_OK ? null
: intent.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
infoTextView =(TextView) findViewById(R.id.infoTextView);
bottomNavigationView = (BottomNavigationView)
findViewById(R.id.bottom);
bottomNavigationView.setOnNavigationItemSelectedListener(new
BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
if (item.getItemId() ==R.id.startItem){
infoTextView.setText(R.string.start);
}else if (item.getItemId() ==R.id.secondItem){
infoTextView.setText(R.string.second);
}else if (item.getItemId() ==R.id.thirdItem){
infoTextView.setText(R.string.third);
}else if (item.getItemId() ==R.id.fourthItem){
infoTextView.setText(R.string.fourth);
}else if (item.getItemId() ==R.id.fifthItem){
infoTextView.setText(R.string.fifth);
}
return false;
}
});
mProgressDialog = new ProgressDialog(this);
progressBar = new ProgressBar(this);
web = (WebView) findViewById(R.id.webView);
WebSettings webSettings= web.getSettings();
web.setVisibility(View.VISIBLE);
web = new WebView(this);
web.getSettings().setJavaScriptEnabled(true);
//enable webview perfomance
web.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
web.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webSettings.setDomStorageEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setUseWideViewPort(true);
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setEnableSmoothTransition(true);
web.loadUrl("http://www.google.com");
web.setWebViewClient(new MyAppWebViewClient());
web.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
if (progress == 100) {
progressBar.setVisibility(View.GONE);
} else {
progressBar.setVisibility(View.VISIBLE);
}
}
//The undocumented magic method override
//Eclipse will swear at you if you try to put @Override here
// For Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
MainActivity.this.startActivityForResult(Intent.createChooser(i,
"File Chooser"), FILECHOOSER_RESULTCODE);
}
// For Android 3.0+
public void openFileChooser(ValueCallback uploadMsg, String
acceptType) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
MainActivity.this.startActivityForResult(
Intent.createChooser(i, "File Browser"),
FILECHOOSER_RESULTCODE);
}
//For Android 4.1
public void openFileChooser(ValueCallback<Uri> uploadMsg, String
acceptType, String capture) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
MainActivity.this.startActivityForResult(Intent.createChooser
(i, "File Chooser"), MainActivity.FILECHOOSER_RESULTCODE);
}
});
setContentView(web);
}
public class MyAppWebViewClient extends WebViewClient {
//ProgressDialogue
ProgressDialog pd = null;
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
pd=new ProgressDialog(MainActivity.this);
pd.setTitle("Chargement");
pd.setMessage("Veuillez patienter...");
pd.show();
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
// mProgressDialog.dismiss();
pd.dismiss();
animate(view);
view.setVisibility(View.VISIBLE);
super.onPageFinished(view, url);
}
private void animate(final WebView view) {
Animation anim = AnimationUtils.loadAnimation(getBaseContext(),
android.R.anim.slide_in_left);
view.startAnimation(anim);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
/* {
view.setVisibility(View.GONE);
mProgressDialog.setTitle("Chargement");
mProgressDialog.show();
mProgressDialog.setMessage("Veuillez patienter... " );
}*/
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL,
Uri.parse(url));
startActivity(intent);
}else if(url.startsWith("http:") || url.startsWith("https:")) {
view.loadUrl(url);
}
//Load the dialer
else if (url.startsWith(WebView.SCHEME_TEL)) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(url));
startActivity(intent);
} else if (url.startsWith(WebView.SCHEME_TEL) ||
url.startsWith("facebook://") ||
url.startsWith("whatsapp://") ||
url.startsWith(WebView.SCHEME_MAILTO) ||
url.startsWith(WebView.SCHEME_GEO) ||
url.startsWith("maps:")) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}return true;
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
web.loadUrl("file:///android_asset/error.html");
super.onReceivedError(view, request, error);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{ //if back key is pressed
if((keyCode == KeyEvent.KEYCODE_BACK)&& web.canGoBack())
{
web.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
// set title
alertDialogBuilder.setTitle("Fermer");
// set dialog message
alertDialogBuilder
.setMessage("Voulez-vous vraiment quitter ?")
.setCancelable(false)
.setPositiveButton("Oui", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish();
}
})
.setNegativeButton("Non", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
}
答案 0 :(得分:0)
试试这个xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
xmlns:design="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity"
android:theme="@style/Theme.AppCompat.Light"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webView"
android:layout_above="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
design:menu="@menu/menu_navigation"
/>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
希望它有所帮助!